2

閱讀django-endless-pagination的文檔它說你可以通過使用@page_template()裝飾器將它的Ajax分頁功能擴展到基於類的視圖。 。 我一直在嘗試使用以實施裝飾的像一小時:使用django-endless-pagination自定義基於擴展ListView的基於c的視圖

class ExtendedListView(ListView): 
    template_name = 'global_template.html' 

    @method_decorator(@page_template('path_to_updatable_content_only_template')) 
    def dispatch(self, *args, **kwargs): 
     return super(ExtendedListView, self).dispatch(*args, **kwargs) 

視圖功能不輸出任何錯誤,但是當我去到另一個網頁加載目標中的「global_template」和不是在裝飾器中定義的模板。

如果有人知道這個實現是否真正起作用,並且我犯了一些錯誤,請指出,我很樂意以正確的方式使用它。

我已經設法拿出一個workarround所以如果someoene的得到了同樣的問題,有沒有標準的答案,這一點,你可以這樣做:

class ExtendedListView(ListView): 
    template_name='global_template_path' 

    ''' 
    render_to_response ¿hack? so that i can render only the updatable DOM part template 
    ''' 
    def render_to_response(self, context): 
     if self.request.is_ajax(): 
      self.template_name = 'path_to_updatable_content_only_template' 
      return super(ExtendedListView, self).render_to_response(context) 
     else: 
      return super(ExtendedListView, self).render_to_response(context) 

乾杯!

回答

3

正式,你可以使用AjaxListView做這個任務:

# views.py 
from endless_pagination.views import AjaxListView  
class BookView(AjaxListView): 
    context_object_name = "books" 
    model = Book 

    template_name = 'books.html' 
    page_template = 'books_list.html' 

在book.html:

{% extends 'base.html' %} 


{% block js %} 
    {{ block.super }} 
    <script src="/static/js/jquery.js" type="text/javascript" charset="utf-8"></script> 
    <script src="/static/js/endless.js" type="text/javascript" charset="utf-8"></script> 
{% endblock %} 

{% block content %} 

<div class="endless_page_template"> 

    {% include page_template %} 
</div> 

{% endblock %} 

,這是books_list.html

{% load endless %} 

{% paginate books %} 

{% for book in books %} 
    {{ book.title }} 
{% endfor %} 

<div class="pagination"> 

    {% show_pages %} 
</div> 
+0

我的問題是試圖讓多個分頁工作。 – acjay 2012-09-26 22:06:20

1

它實際上相當令人費解的Ajax實現如何工作。我必須推出我自己的解決方案,因爲我想使用通用視圖,Ajax和多個分頁。爲了弄清楚如何使它工作,我必須對示例中的代碼,django-endless-pagination裝飾器和Django的視圖本身進行逆向工程。在滾動我自己的解決方案的過程中,我簡化了一些東西,但可能會進一步簡化。也許這可能是對別人有用:

class SearchView(View): 
    """ 
    Based on code from django-endless-pagination, modified for multiple 
    pagination views on the same page 
    """ 

    template = 'app/search.html' 
    page_templates = {'object1Page': 'app/search_object1_pane.html', 
    'object2Page': 'app/search_object2_pane.html'} 

    def get_context_data_and_template(self, **kwargs): 
     context = {'params': kwargs} 

     # Check whether AJAX has made a request, if so, querystring_key will be 
     # set, identifying which paginator to render 
     querystringKey = self.request.REQUEST.get('querystring_key') 
     template = self.page_templates.get(querystringKey) 
     # switch template on ajax requests 
     if not (self.request.is_ajax() and template): 
      template = self.template  
     context['page_template'] = template 

     # Always generate the QuerySets that will be paginated 
     if self.request.GET['query']: 
      searchTerm = self.request.GET['query'] 
     else: 
      searchTerm = kwargs['search'] 

     # *** Insert your search code here *** 

     context['object1Results'] = object1QuerySet 
     context['object2Results'] = object2QuerySet 

     extraContext = self.kwargs.get("extra_context", {}) 
     context.update(extraContext) 

     return context, template 

    def get(self, request, *args, **kwargs): 
     context, template = self.get_context_data_and_template(**kwargs) 
     return TemplateResponse(request=self.request, 
      template=template, 
      context=context)