对象无法通过 django 的分页进行迭代
我有一个显示事件列表的模板。为了准备事件列表,我使用通用视图,并设置“paginate_by”参数。奇怪的是,当我加载页面时,我看到:
TemplateSyntaxError at /event/latest/
Caught an exception while rendering: 'int' object is not iterable
在 pagination.html 模板的第 9 行:
{% if is_paginated %}
{% load i18n %}
<div class="pagination">
{% if page_obj.has_previous %}
<a href="?page={{ page_obj.previous_page_number }}{{ getvars }}" class="prev"><< Prev</a>
{% else %}
<span class="disabled prev"><< Prev</span>
{% endif %}
#here {% for page in pages %}
{% if page %}
{% ifequal page page_obj.number %}
<span class="current page">{{ page }}</span>
{% else %}
<a href="?page={{ page }}{{ getvars }}" class="page">{{ page }}</a>
{% endifequal %}
{% else %}
...
{% endif %}
{% endfor %}
{% if page_obj.has_next %}
<a href="?page={{ page_obj.next_page_number }}{{ getvars }}" class="next">Next >></a>
{% else %}
<span class="disabled next">Next >></span>
{% endif %}
</div>
{% endif %}
这是我的视图:
def events_nearest(request):
events = Event.nearest.all()
return object_list(request,
queryset = events,
extra_context = {'title': 'Nearest events'},
paginate_by = 12,
template_name = 'event/events_date.html')
和模型:
class NearestManager(models.Manager):
def get_query_set(self):
return super(NearestManager, self).get_query_set().order_by('-data')
class Event(models.Model):
category = models.ForeignKey(Category)
title = models.CharField(max_length=120)
slug = models.SlugField(max_length=255, unique=True, verbose_name='Slug')
about = models.TextField()
city = models.ForeignKey(City)
objects = models.Manager()
nearest = NearestManager()
有什么想法会导致这种情况吗?
I have a template showing a list of events. To prepare list of events I'm using generic views, and set 'paginate_by' parameter. Strangely when I load my page I see :
TemplateSyntaxError at /event/latest/
Caught an exception while rendering: 'int' object is not iterable
in 9th line of pagination.html template :
{% if is_paginated %}
{% load i18n %}
<div class="pagination">
{% if page_obj.has_previous %}
<a href="?page={{ page_obj.previous_page_number }}{{ getvars }}" class="prev"><< Prev</a>
{% else %}
<span class="disabled prev"><< Prev</span>
{% endif %}
#here {% for page in pages %}
{% if page %}
{% ifequal page page_obj.number %}
<span class="current page">{{ page }}</span>
{% else %}
<a href="?page={{ page }}{{ getvars }}" class="page">{{ page }}</a>
{% endifequal %}
{% else %}
...
{% endif %}
{% endfor %}
{% if page_obj.has_next %}
<a href="?page={{ page_obj.next_page_number }}{{ getvars }}" class="next">Next >></a>
{% else %}
<span class="disabled next">Next >></span>
{% endif %}
</div>
{% endif %}
Here is my view :
def events_nearest(request):
events = Event.nearest.all()
return object_list(request,
queryset = events,
extra_context = {'title': 'Nearest events'},
paginate_by = 12,
template_name = 'event/events_date.html')
And model :
class NearestManager(models.Manager):
def get_query_set(self):
return super(NearestManager, self).get_query_set().order_by('-data')
class Event(models.Model):
category = models.ForeignKey(Category)
title = models.CharField(max_length=120)
slug = models.SlugField(max_length=255, unique=True, verbose_name='Slug')
about = models.TextField()
city = models.ForeignKey(City)
objects = models.Manager()
nearest = NearestManager()
Any ideas what can cause this ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
pages
变量是页数,它是 int,因此错误:“int”对象不可迭代,您应该循环
page_range
pages
variable is the number of pages, which is int and hence the error: 'int' object is not iterableyou should be looping over
page_range
我遇到了同样的错误。有一个注释位于
https://docs.djangoproject.com/en/dev/topics/pagination/
在 Django 开发版本中进行了更改:以前,您需要使用
{% for contact in contacts.object_list %}
,因为 Page 对象不可迭代。因此
{% for page in pages.object_list %}
可能可以解决您的问题。I met the same error. There is a note at
https://docs.djangoproject.com/en/dev/topics/pagination/
Changed in Django Development version: Previously, you would need to use
{% for contact in contacts.object_list %}
, since the Page object was not iterable.So
{% for page in pages.object_list %}
could probably solve your problem.对于任何偶然发现这篇文章的人:
与 Django 1.4 及更高版本(据我所知)一样,用于分页的可迭代对象现在是
paginator.page_range
,即 for 循环应该是For anybody stumbled upon this post:
As with Django 1.4 and later (as far as I know), the iterable object for pagination is now
paginator.page_range
, i.e. the for loop should be在你的错误行 #9
{% for page in pages %}
到底是什么pages
在你的代码中的任何地方都看不到它。
In your error line #9
{% for page in pages %}
what exactly ispages
Can't see it in your code anywhere.