如何在基于 Django 类的通用 ListViews 中使用分页? [英] How do I use pagination with Django class based generic ListViews?

查看:36
本文介绍了如何在基于 Django 类的通用 ListViews 中使用分页?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 Django 1.3 中使用分页?

文档对此不是很清楚.

  • 我的 views.py 有什么用?

  • 我的模板有什么用?

  • 我的 URLconf 文件会发生什么?

解决方案

我想您会询问有关在新的基于类的视图中使用分页的信息,因为使用传统的基于函数的视图很容易找到.我发现仅仅通过设置 pagination_by 变量就足以激活分页.参见 基于类的通用视图.

例如,在您的 views.py 中:

导入模型从 django.views.generic 导入 ListView类 CarListView(ListView):model = models.Car # 设置 queryset = models.Car.objects.all() 的简写template_name = 'app/car_list.html' # 可选(默认是 app_name/modelNameInLowerCase_list.html;它会在你的模板文件夹中查找该路径和文件)context_object_name = "car_list" #default 是 object_list 以及模型的_verbose_name_list 和/或模型的_verbose_name_plural_list,如果在模型的内部元类中定义paginate_by = 10 #就是这样!!

在您的模板 (car_list.html) 中,您可以包含这样的分页部分(我们有一些可用的上下文变量:is_paginatedpage_obj, 和 paginator).

{# .... **普通内容列表,可能是表格** .... #}{% if car_list %}<table id="汽车">{% for car_list %}<tr><td>{{ car​​.model }}</td><td>{{ car​​.year }}</td><td><a href="/car/{{ car​​.id }}/" class="see_detail">detail</a></td></tr>{% 结束为 %}{# .... **现在是分页部分** .... #}{% if is_paginated %}<div class="分页"><span class="page-links">{% if page_obj.has_previous %}<a href="/cars?page={{ page_obj.previous_page_number }}">previous</a>{% 万一 %}<span class="page-current">第 {{ page_obj.number }} 页,共 {{ page_obj.paginator.num_pages }}.</span>{% if page_obj.has_next %}<a href="/cars?page={{ page_obj.next_page_number }}">next</a>{% 万一 %}</span>

{% 万一 %}{% 别的 %}<h3>我的汽车</h3><p>没有找到汽车!!!:(</p>{% 万一 %}{# .... **更多内容、页脚等** .... #}

要显示的页面由 GET 参数指示,只需将 ?page=n 添加到 URL.

How do I use pagination with Django 1.3?

The documentation is not very clear on this.

  • What goes to my views.py?

  • What goes to my template?

  • What goes to my URLconf file?

解决方案

I think you ask for information about using pagination with the new class based views since, with traditional function based views, it is easy to find. I found that just by setting the paginate_by variable is enough to activate the pagination. See in Class-based generic views.

For example, in your views.py:

import models
from django.views.generic import ListView

class CarListView(ListView):
    model = models.Car      # shorthand for setting queryset = models.Car.objects.all()
    template_name = 'app/car_list.html'  # optional (the default is app_name/modelNameInLowerCase_list.html; which will look into your templates folder for that path and file)
    context_object_name = "car_list"    #default is object_list as well as model's_verbose_name_list and/or model's_verbose_name_plural_list, if defined in the model's inner Meta class
    paginate_by = 10  #and that's it !!

In your template (car_list.html), you can include a pagination section like this (we have some context variables available: is_paginated, page_obj, and paginator).

{# .... **Normal content list, maybe a table** .... #}
{% if car_list %}
    <table id="cars">
        {% for car in car_list %}
            <tr>
                <td>{{ car.model }}</td>
                <td>{{ car.year }}</td>
                <td><a href="/car/{{ car.id }}/" class="see_detail">detail</a></td>
            </tr>
        {% endfor %}
    </table>
    {# .... **Now the pagination section** .... #}
    {% if is_paginated %}
        <div class="pagination">
            <span class="page-links">
                {% if page_obj.has_previous %}
                    <a href="/cars?page={{ page_obj.previous_page_number }}">previous</a>
                {% endif %}
                <span class="page-current">
                    Page {{ page_obj.number }} of {{ page_obj.paginator.num_pages }}.
                </span>
                {% if page_obj.has_next %}
                    <a href="/cars?page={{ page_obj.next_page_number }}">next</a>
                {% endif %}
            </span>
        </div>
    {% endif %}
{% else %}
    <h3>My Cars</h3>
    <p>No cars found!!! :(</p>
{% endif %}
{# .... **More content, footer, etc.** .... #}

The page to display is indicated by a GET parameter, simply adding ?page=n, to the URL.

这篇关于如何在基于 Django 类的通用 ListViews 中使用分页?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆