如何在基于类的通用视图中使用分页?

发布于 2024-11-27 03:42:45 字数 1277 浏览 1 评论 0原文

我尝试对基于类的通用视图实现分页,但以我的方式实现,它不起作用。

urls

url(r'^cat/(?P<category>[\w+\s]*)/page(?P<page>[0-9]+)/$',
    CategorizedPostsView.as_view(), {'paginate_by': 3}),

view

class CategorizedPostsView(ListView):
    template_name = 'categorizedposts.djhtml'
    context_object_name = 'post_list'

    def get_queryset(self):
        cat = unquote(self.kwargs['category'])
        category = get_object_or_404(ParentCategory, category=cat)
        return category.postpages_set.all()

template

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

        <span class="current">
            Page {{ post_list.number }} of {{ post_list.paginator.num_pages }}.
        </span>

        {% if post_list.has_next %}
            <a href="?page={{ post_list.next_page_number }}">next</a>
        {% endif %}
    </span>
</div>

当我尝试获取 http:// 127.0.0.1:8000/cat/category_name/?page=1 甚至 http:// 127.0.0.1:8000/cat/category_name/ 时,出现 404 异常。

如何在基于类的通用视图中正确使用分页?

I try to implement pagination to class-based generic view and in way I did it, it's not works.

urls

url(r'^cat/(?P<category>[\w+\s]*)/page(?P<page>[0-9]+)/

view

class CategorizedPostsView(ListView):
    template_name = 'categorizedposts.djhtml'
    context_object_name = 'post_list'

    def get_queryset(self):
        cat = unquote(self.kwargs['category'])
        category = get_object_or_404(ParentCategory, category=cat)
        return category.postpages_set.all()

template

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

        <span class="current">
            Page {{ post_list.number }} of {{ post_list.paginator.num_pages }}.
        </span>

        {% if post_list.has_next %}
            <a href="?page={{ post_list.next_page_number }}">next</a>
        {% endif %}
    </span>
</div>

When I try to get the http:// 127.0.0.1:8000/cat/category_name/?page=1 or even http:// 127.0.0.1:8000/cat/category_name/ I got 404 exception.

How to use pagination in class-based generic views in right way?

, CategorizedPostsView.as_view(), {'paginate_by': 3}),

view

template

When I try to get the http:// 127.0.0.1:8000/cat/category_name/?page=1 or even http:// 127.0.0.1:8000/cat/category_name/ I got 404 exception.

How to use pagination in class-based generic views in right way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

栩栩如生 2024-12-04 03:42:45

嘿,ListView 已经有一个 kwarg paginate_by,所以只需将其传入即可。
尝试这样的事情:

url(r'^cat/(?P<category>[\w+\s]*)/page(?P<page>[0-9]+)/

对于你的模板,你可以尝试这样的事情:

{% if is_paginated %}
    <div class="pagination">
        <span class="step-links">
            {% if page_obj.has_previous %}
                <a href="?page={{ page_obj.previous_page_number }}">previous</a>
            {% endif %}

            <span class="current">
                Page {{ page_obj.number }} of {{ paginator.num_pages }}.
            </span>

            {% if page_obj.has_next %}
                <a href="?page={{ page_obj.next_page_number }}">next</a>
            {% endif %}
        </span>
    </div>
{% endif %}
, CategorizedPostsView.as_view(paginate_by=3)),

对于你的模板,你可以尝试这样的事情:

hey there is already a kwarg paginate_by for the ListView so just pass it in.
try something like this:

url(r'^cat/(?P<category>[\w+\s]*)/page(?P<page>[0-9]+)/

and for your template you could try something like:

{% if is_paginated %}
    <div class="pagination">
        <span class="step-links">
            {% if page_obj.has_previous %}
                <a href="?page={{ page_obj.previous_page_number }}">previous</a>
            {% endif %}

            <span class="current">
                Page {{ page_obj.number }} of {{ paginator.num_pages }}.
            </span>

            {% if page_obj.has_next %}
                <a href="?page={{ page_obj.next_page_number }}">next</a>
            {% endif %}
        </span>
    </div>
{% endif %}
, CategorizedPostsView.as_view(paginate_by=3)),

and for your template you could try something like:

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