对象无法通过 django 的分页进行迭代

发布于 2024-09-11 21:07:15 字数 2124 浏览 16 评论 0原文

我有一个显示事件列表的模板。为了准备事件列表,我使用通用视图,并设置“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">&lt;&lt; Prev</a>
    {% else %}
        <span class="disabled prev">&lt;&lt; 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 &gt;&gt;</a>
    {% else %}
        <span class="disabled next">Next &gt;&gt;</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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

夜空下最亮的亮点 2024-09-18 21:07:15

pages 变量是页数,它是 int,因此错误:“int”对象不可迭代,

您应该循环 page_range

{% for page in page_range %}

pages variable is the number of pages, which is int and hence the error: 'int' object is not iterable

you should be looping over page_range

{% for page in page_range %}
葬花如无物 2024-09-18 21:07:15

我遇到了同样的错误。有一个注释位于
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.

若水微香 2024-09-18 21:07:15

对于任何偶然发现这篇文章的人:

与 Django 1.4 及更高版本(据我所知)一样,用于分页的可迭代对象现在是 paginator.page_range ,即 for 循环应该是

{% for page_num in paginator.page_range %}

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

{% for page_num in paginator.page_range %}
丶情人眼里出诗心の 2024-09-18 21:07:15

在你的错误行 #9 {% for page in pages %} 到底是什么 pages

在你的代码中的任何地方都看不到它。

In your error line #9 {% for page in pages %} what exactly is pages

Can't see it in your code anywhere.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文