2011-05-06 49 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

相關問題