gpt4 book ai didi

django - "page"GET参数从何而来?

转载 作者:行者123 更新时间:2023-12-01 23:38:05 25 4
gpt4 key购买 nike

我是 Django 的初学者,我对 Django 引擎盖下的工作原理深思熟虑。我目前正在我的网络应用程序中实现分页。

看看这个 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});

request.GET 不是一个包含 url 中所有 GET 请求参数的字典对象,以及用于返回值的 .get() 方法对于参数内的给定键?当我启动应用程序时,我的 URL 当前只是 localhost:8000,为什么如果我传递键“page”它会起作用?

我的 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 %}

我的 pagination.html 文件:

<!-- 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>

最佳答案

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

# page will be None
page = request.GET.get("page")

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

try:
# Passing None here
posts = paginator.page(page)
except PageNotAnInteger:

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

try:
posts = paginator.page(page) # Raises PageNotAnInteger because None passed
except PageNotAnInteger:
# Posts are retrieved for page 1
posts = paginator.page(1)

来自上述调用的 postspage 的值(仍然是 None)然后被传递给模板。

return render(request, "post/list.html", {"page": page, "posts": posts});

然后模板 list.html 迭代帖子并显示它们。

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

<!-- Pass the value of posts using a variable name of page -->
{% include "pagination.html" with page=posts %}

所以pagination.html模板中引用page的地方,实际上是在使用posts的值。

<!-- Really posts.number and posts.paginator.num_pages -->
Page {{ page.number }} of {{ page.paginator.num_pages }}

希望这有助于解释事情。

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

关于django - "page"GET参数从何而来?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49580009/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com