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'을로드하고 데코레이터에 정의 된 템플릿이 아닙니다.

누군가가이 구현이 실제로 작동하는지 실수로 지적하고 싶습니다. 올바른 방법을 사용하게되어 기쁩니다.

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) 

건배 :이 같은 문제를 가지고 당신이 할 수있는이 아무 준수 응답이 없습니다 someoene의, 그래서 만약

나는 workarround을 마련하기 위해 관리했습니다! book.html에서

# 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' 

:

답변

3

공식적으로이 작업을 수행하기 위해 AjaxListView을 사용할 수 있습니다

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

1

입니다 Ajax 구현이 어떻게 작동하는지 매우 복잡합니다. 내가 일반적인 뷰, Ajax 및 다중 페이지 매김을 사용하고 싶었 기 때문에 내 솔루션을 롤업해야했습니다. 어떻게 작동하는지 알아 내려면 예제, 장고 - 끝없이 페이지 매기기 데코레이터 및 장고의 견해에서 코드를 리버스 엔지니어링해야했습니다. 내 솔루션을 선전하는 과정에서 조금 단순화되었지만 단순화 될 수 있습니다. 아마도 이것은 다른 사람에게 유용 할 수 있습니다.

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)