Python 为什么我的django模板没有输出模板变量对象?
我在获取模板以输出表示博客文章的Python 为什么我的django模板没有输出模板变量对象?,python,django,django-models,django-templates,django-views,Python,Django,Django Models,Django Templates,Django Views,我在获取模板以输出表示博客文章的Post对象时遇到问题。在我的主索引页面上,我的博客帖子显示得很好,加载模板变量也没有问题。当用户单击其中一个存档日期(表示存档链接以显示某一天和某个月的帖子)时,将使用相同的索引页,但会指定一组不同的帖子来显示,并按日期过滤 视图.py def month(request, year, month): """Monthly archive.""" posts = Post.objects.filter(created__year=year, cr
Post
对象时遇到问题。在我的主索引页面上,我的博客帖子显示得很好,加载模板变量也没有问题。当用户单击其中一个存档日期(表示存档链接以显示某一天和某个月的帖子)时,将使用相同的索引页,但会指定一组不同的帖子来显示,并按日期过滤
视图.py
def month(request, year, month):
"""Monthly archive."""
posts = Post.objects.filter(created__year=year, created__month=month)
d = dict(posts=posts, user=request.user, months=mkmonth_lst(), archive=True)
return render(request, 'blog/index.html', d)
index.html
{% for post in posts.object_list %}
<div class="blogpost">
<div class="blogpost-title"><h2>{{ post.title }}<h2></div>
<div class="blogpost-meta"><h4>{{ post.created }}</h></div>
<div class="blogpost-body">{{ post.body|linebreaks }}</div>
<div class="blogpost-comments"><a href="{% url 'post' post.id %}">Comments</a></div>
</div>
{% endfor %}
{%for posts in posts.object_list%}
{{post.title}}
{{post.created}}
{{post.body | linebreaks}}
{%endfor%}
在上个月的函数中,在检索Post
对象列表并对它们进行计数后,我可以看到它们正在被检索。问题是索引页没有输出它们
有人能帮忙吗?谢谢在模板中,您应该循环浏览
帖子
,而不是帖子。对象列表
谢谢!这很奇怪,我最初使用的是posts.object_list
来输出我的整个posts集合,但在输出posts子集时它不起作用。您是在另一个视图中使用的,还是使用基于类的视图进行分页?是的,另一个视图使用分页,但与months
共享模板。仅使用posts
,分页似乎仍然有效,我在哪里读到过使用posts.object\u list
的原因吗?对象列表不会循环通过object\u list
,因此我认为循环通过posts
对您有效。