2016-11-01 72 views
3

我對W,是相當新的,我正在創建一個網站,將有一個資源(博客)部分的過程,我不知道如何實現分頁,以便只有5個職位在每個頁面上,用戶必須點擊一個號碼(1,2,3等)才能進入下一頁,以查看接下來的5個帖子。W Pa分頁

我有這個在我的資源/博客索引頁的分頁部分模板:

<ul class="pagination"> 
    <li><a href="#"><i class="fa fa-angle-left"></i></a></li> 
    <li class="active"><a href="#">1</a></li> 
    <li><a href="#">2</a></li> 
    <li><a href="#">3</a></li> 
    <li><a href="#"><i class="fa fa-angle-right"></i></a></li> 
</ul> 

什麼代碼,我需要合併,使這個功能?提前致謝。

回答

8

Django爲此提供了模塊django.core.paginatorhttps://docs.djangoproject.com/en/1.10/topics/pagination/。在Wagtail中使用它與Django文檔中的示例非常相似 - 唯一真正的區別是,當您設置要傳遞給模板的Paginator對象時,可以使用頁面模型中的get_context方法的視圖功能。您的模型定義將是這個樣子:

from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger 

class ResourceIndexPage(Page): 
    # ... 
    def get_context(self, request): 
     context = super(ResourceIndexPage, self).get_context(request) 

     # Get the full unpaginated listing of resource pages as a queryset - 
     # replace this with your own query as appropriate 
     all_resources = ResourcePage.objects.live() 

     paginator = Paginator(all_resources, 5) # Show 5 resources per page 

     page = request.GET.get('page') 
     try: 
      resources = paginator.page(page) 
     except PageNotAnInteger: 
      # If page is not an integer, deliver first page. 
      resources = paginator.page(1) 
     except EmptyPage: 
      # If page is out of range (e.g. 9999), deliver last page of results. 
      resources = paginator.page(paginator.num_pages) 

     # make the variable 'resources' available on the template 
     context['resources'] = resources 

     return context 

在您的模板,現在你可以使用{% for resource in resources %}遍歷所有的項目,並顯示分頁鏈接如下:

<ul class="pagination"> 
    {% if resources.has_previous %} 
    <li><a href="?page={{ resources.previous_page_number }}"><i class="fa fa-angle-left"></i></a></li> 
    {% endif %} 
    {% for page_num in resources.paginator.page_range %} 
    <li {% if page_num == resources.number %}class="active"{% endif %}><a href="?page={{ page_num }}">{{ page_num }}</a></li> 
    {% endfor %} 
    <li><a href="?page={{ resources.next_page_number }}"><i class="fa fa-angle-right"></i></a></li> 
</ul> 
+3

只是想補充一點,我在最後一個'

  • '附近使用了以下內容,如果有下一個頁面只顯示下一個箭頭 - '{%if resources.has_next%} {%endif%}' – Kim

    +0

    Nice 。這工作得很好。有什麼方法可以更新這個,所以我可以使用有/ page/1/in的好網址嗎?目前我正在竊聽,但它非常混亂! – Designer023

    +0

    @ Designer023查看'RoutablePageMixin':http://docs.wagtail.io/en/v1.13.1/reference/contrib/routablepage.html – gasman

    -1

    我已經試過這幾次(總新手在這裏),我還沒有得到它的工作。我的博客索引被稱爲NewsIndexPage(如下):

    class NewsIndexPage(Page): 
    intro = RichTextField(blank=True) 
    
    def get_context(self, request): 
        # Update context to include only published posts, ordered by reverse-chron 
        context = super(NewsIndexPage, self).get_context(request) 
        blogpages = self.get_children().live().order_by('-first_published_at') 
        context['blogpages'] = blogpages 
        return context 
    

    如何將paginantion代碼應用到我的頁面?對不起,這是一個不容小覷的過程 - 我仍然在學習Python,以及如何將它應用到Django。

    +0

    我建議這個給你: '高清GET_CONTEXT(個體經營,請求): 上下文=超(NewsIndexPage,個體經營).get_context(請求) blogpages = self.get_children()生活()ORDER_BY(' - 。 first_published_at ') 分頁程序=分頁程序(blogpages,5)#顯示每 頁 頁= request.GET.get(5個資源' 頁「) 嘗試: 博客= paginator.page(頁) 除了PageNotAnInteger: 博客= paginator。(1) 除EmptyPage: blogs = paginator.page(paginator.num_pages) 上下文['blogs'] =博客 return context' – Kim

    +0

    謝謝!我今晚會嘗試。我曾嘗試調整原始代碼,但沒有發佈它,因爲我知道這是錯誤的。看看你的代碼,我明白了爲什麼! – Steve

    3

    我非常感謝你讓我在這裏 - 非常感謝你的幫助。我不得不做一些調整才能使它工作。這裏的模型,如果有人遇到了同樣的問題:

    class NewsIndexPage(Page): 
    intro = RichTextField(blank=True) 
    
    def get_context(self, request): 
        context = super(NewsIndexPage, self).get_context(request) 
    
        # Get the full unpaginated listing of resource pages as a queryset - 
        # replace this with your own query as appropriate 
        blogpages = self.get_children().live().order_by('-first_published_at') 
    
        paginator = Paginator(blogpages, 3) # Show 3 resources per page 
    
        page = request.GET.get('page') 
        try: 
         blogpages = paginator.page(page) 
        except PageNotAnInteger: 
         # If page is not an integer, deliver first page. 
         blogpages = paginator.page(1) 
        except EmptyPage: 
         # If page is out of range (e.g. 9999), deliver last page of results. 
         blogpages = paginator.page(paginator.num_pages) 
    
        # make the variable 'resources' available on the template 
        context['blogpages'] = blogpages 
    
        return context 
    

    ...和這裏的HTML:

    <ul class="pagination"> 
        {% if blogpages.has_previous %} 
         <li> 
         <a href="?page={{ blogpages.previous_page_number }}"><i class="fa fa-angle-left"></i></a> 
         </li> 
        {% endif %} 
        {% for page_num in blogpages.paginator.page_range %} 
         <li {% if page_num == blogpages.number %} class="active"{% endif %}> 
         <a href="?page={{ page_num }}">{{ page_num }}</a> 
         </li> 
        {% endfor %} 
        {% if resources.has_next %} 
         <li> 
         <a href="?page={{ blogpages.next_page_number }}"><i class="fa fa-angle-right"></i></a> 
         </li> 
         {% endif %} 
        </ul> 
    

    它的工作原理就像一個魅力 - 並增加了學習曲線!