如何使用基于Django类的genericsListView分页?
我如何使用Django 1.3分页?
这个文件不是很清楚。
-
什么去我的
views.py
? -
什么去我的模板?
-
什么去我的URLconf文件?
我认为你需要了解使用基于新的基于类的视图的分页信息,因为使用传统的基于function的视图很容易find。 我发现只要设置paginate_by
variables就足以激活分页。 请参阅基于类的通用视图 。
例如,在您的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 !!
在您的模板( car_list.html
)中,您可以包含像这样的分页部分(我们有一些上下文variables可用: is_paginated
, page_obj
和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.** .... #}
要显示的页面由GET参数表示,只需将?page=n
添加到URL即可。
假设,我有一个名为FileExam(models.Model)
app / models.py中的类:
应用程序/ models.py
class FileExam(models.Model): myfile = models.FileField(upload_to='documents/%Y/%m/%d') date = models.DateTimeField(auto_now_add=True, blank=True) teacher_name = models.CharField(max_length=30) status = models.BooleanField(blank=True, default=False)
应用程序/ views.py
from app.models import FileExam from django.core.paginator import Paginator from django.core.paginator import EmptyPage from django.core.paginator import PageNotAnInteger class FileExamListView(ListView): model = FileExam template_name = "app/exam_list.html" paginate_by = 10 def get_context_data(self, **kwargs): context = super(SoalListView, self).get_context_data(**kwargs) list_exam = FileExam.objects.all() paginator = Paginator(list_exam, self.paginate_by) page = self.request.GET.get('page') try: file_exams = paginator.page(page) except PageNotAnInteger: file_exams = paginator.page(1) except EmptyPage: file_exams = paginator.page(paginator.num_pages) context['list_exams'] = file_exams return context
只是在get_context_data
稍加改动,并在这里添加了来自django文档的分页代码
应用程序/模板/应用/ exam_list.html
正常的内容列表
<table id="exam"> {% for exam in list_exams %} <tr> <td>{{ exam.myfile }}</td> <td>{{ exam.date }}</td> <td>.....</td> </tr> {% endfor %} </table>
分页部分
{% if is_paginated %} <ul class="pagination"> {% if page_obj.has_previous %} <li> <span><a href="?page={{ page_obj.previous_page_number }}">Previous</a></span> </li> {% endif %} <li class=""> <span>Page {{ page_obj.number }} of {{ page_obj.paginator.num_pages }}.</span> </li> {% if page_obj.has_next %} <li> <span><a href="?page={{ page_obj.next_page_number }}">Next</a></span> </li> {% endif %} </ul> {% else %} <h3>Your File Exam</h3> <p>File not yet available</p> {% endif %}
应用程序/ urls.py
urlpatterns = [ url( r'^$', views.FileExamListView.as_view(), name='file-exam-view'), ), ... ]