Django “在哪里?”;第页“;获取参数的来源?

Django “在哪里?”;第页“;获取参数的来源?,django,django-models,Django,Django Models,我是Django的初学者,我对Django引擎盖下的实际工作非常关心。我目前正在我的web应用程序中实现分页 看看这个view.py文件: def post_list(request): object_list = Post.published.all(); # '.published' is a manager. paginator = Paginator(object_list, 3); # 3 posts in each page. page = request

我是Django的初学者,我对Django引擎盖下的实际工作非常关心。我目前正在我的web应用程序中实现分页

看看这个view.py文件:

def post_list(request):

    object_list = Post.published.all(); # '.published' is a manager.

    paginator = Paginator(object_list, 3); # 3 posts in each page.
    page = request.GET.get("page");

    try:

        posts = paginator.page(page);

    except PageNotAnInteger:

        posts = paginator.page(1);

    except EmptyPage:

        posts = paginator.page(paginator.num_pages);

    return render(request, "post/list.html", {"page": page, "posts": posts});
不是请求。获取包含url中所有GET请求参数的dictionary对象,以及用于返回值的.GET()方法 对于参数内的给定键?当我启动应用程序时,我的URL当前仅为localhost:8000,如果我传递键“page”,它为什么会工作

My list.html文件:

{% extends "base.html" %}

{% block title %}My Blog{% endblock %}

{% block content %}

  <h1>My Blog</h1>
  {% for post in posts %}
    <h2><a href="{{ post.get_absolute_url }}">{{ post.title }}</a></h2>  <!-- How does this absurl work?-->
    <p class="date">Published {{ post.publish }} by {{ post.author }}</p> <!-- What does '.publish' print?-->
    {{ post.body|truncatewords:30|linebreaks }}
  {% endfor %}

  {% include "pagination.html" with page=posts %}
  <!-- The statement above is the little menu: "Page 1 of 2. Next" -->
  <!-- It also sends the 'page' variable as a GET parameter. -->

{% endblock %}
<!-- This pagination template expects a paginator object. -->
<div class="pagination">

  <span class="step-links">
    {% if page.has_previous %}
      <a href="?page={{ page.previous_page_number }}">Previous</a>
    {% endif %}


    <span class="current">
      Page {{ page.number }} of {{ page.paginator.num_pages }}. <!-- what!? -->
    </span>

    {% if page.has_next %}
      <a href="?page={{ page.next_page_number }}">Next</a>
    {% endif %}

  </span>
</div>
{%extends“base.html”%}
{%block title%}我的博客{%endblock%}
{%block content%}
我的博客
{posts%%中的post为%s}

由{{post.author}发布{{post.publish}

{{post.body | truncatewords:30 | linebreaks}} {%endfor%} {%include“pagination.html”和page=posts%} {%endblock%}
My pagination.html文件:

{% extends "base.html" %}

{% block title %}My Blog{% endblock %}

{% block content %}

  <h1>My Blog</h1>
  {% for post in posts %}
    <h2><a href="{{ post.get_absolute_url }}">{{ post.title }}</a></h2>  <!-- How does this absurl work?-->
    <p class="date">Published {{ post.publish }} by {{ post.author }}</p> <!-- What does '.publish' print?-->
    {{ post.body|truncatewords:30|linebreaks }}
  {% endfor %}

  {% include "pagination.html" with page=posts %}
  <!-- The statement above is the little menu: "Page 1 of 2. Next" -->
  <!-- It also sends the 'page' variable as a GET parameter. -->

{% endblock %}
<!-- This pagination template expects a paginator object. -->
<div class="pagination">

  <span class="step-links">
    {% if page.has_previous %}
      <a href="?page={{ page.previous_page_number }}">Previous</a>
    {% endif %}


    <span class="current">
      Page {{ page.number }} of {{ page.paginator.num_pages }}. <!-- what!? -->
    </span>

    {% if page.has_next %}
      <a href="?page={{ page.next_page_number }}">Next</a>
    {% endif %}

  </span>
</div>

{%如果page.has_previous%}
{%endif%}
第{{Page.paginator.num_pages}页中的第{{Page.number}页。
{%如果page.has_next%}
{%endif%}

当请求中没有参数时(当您点击
http://localhost:8000
直接),页面的
值将为
None
。这是
request.GET.GET()
在找不到您要的密钥时的默认行为,与普通Python字典相同(因为GET扩展了它)

这意味着
None
被传递到
paginator.page()

这可能意味着(尽管我们看不到
paginagor
)会引发
PageNotAnInteger
异常,因此将值1传递给
paginagor.page()

来自上述调用的
帖子
,以及
页面
(仍然是
)的值随后被传递到模板

return render(request, "post/list.html", {"page": page, "posts": posts});
模板
list.html
然后迭代帖子并显示它们

令人困惑的是,当包含
pagination.html
模板时,它将一个名为
page
的上下文变量定义为
posts
的当前值:

<!-- Pass the value of posts using a variable name of page -->
{% include "pagination.html" with page=posts %}
希望这有助于解释事情


还有一件事,您不需要在Python中的每一行末尾添加分号。

这个问题对我来说没有意义。短语“Django怎么可能知道它不应该显示它应该显示的所有内容”在逻辑上是不一致的。非常感谢您的注意。您正在捕获PageNotAnyTeger异常,然后将1传递给paginator.page函数。然后您捕获到一个EmptyPage异常,它看起来像是使用了默认值/回退值。为了更好地理解您的问题,您能解释一下您到底对什么感到困惑吗?是因为您不知道Django从何处获取用于填充请求的键值对。获取字典?好的。我的问题是:如果“request.GET”是一个用GET请求参数填充的字典,那么如果GET请求中没有“page”参数,为什么我可以使用“page”作为“request.GET.GET”(“page”)中的键?
<!-- Pass the value of posts using a variable name of page -->
{% include "pagination.html" with page=posts %}
<!-- Really posts.number and posts.paginator.num_pages -->
Page {{ page.number }} of {{ page.paginator.num_pages }}