2011-05-06 77 views

回答

281

我想你会询问有关使用基于新类的视图分页的信息,因为使用基于传统函数的视图很容易找到。我发现只需设置paginate_by变量就足以激活分页。请参阅Class-based generic views

例如,在你的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),您可以包括这样的分页部分(我们有一些可用的上下文变量:is_paginatedpage_objpaginator)。

{# .... **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。

+1

那好吧,但你怎么绑模板也看到“car_list”对象? – gath 2011-05-06 11:21:35

+0

我添加了一个模板示例 – ervin 2011-05-06 13:09:09

+24

仅供参考,您也可以直接在urls.py:url(r'^cars/$',ListView中执行此操作。as_view( model = Car, paginate_by = 10 )), – shawnwall 2011-08-08 02:42:34

27

假设,我在app/models.py一类名为FileExam(models.Model)

应用程序/ 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的分页代码文档here

应用/模板/应用/ exam_list.html

正常的内容列表

​​

PAGINATE部分

{% 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 %} 

app/urls.py

urlpatterns = [ 
url(
    r'^$', views.FileExamListView.as_view(), name='file-exam-view'), 
), 
... ] 
+0

这看起来不正确:'context = super(SoalListView,self)...'。你的意思是:'context = super(FileExamListView,self)...'? – cezar 2017-09-27 10:26:11

相关问题