【问题标题】:Using django-endless-pagination with a custom c-based view that extends ListView将 django-endless-pagination 与扩展 ListView 的基于 c 的自定义视图一起使用
【发布时间】:2012-09-27 16:00:24
【问题描述】:

阅读django-endless-pagination 的文档,它说您可以通过使用@page_template() 装饰器将它的Ajax 分页功能扩展到基于类的视图... 我一直在尝试使用一个小时来实现该装饰器:

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)

干杯!

【问题讨论】:

    标签: django django-class-based-views django-endless-pagination


    【解决方案1】:

    官方来说,你可以使用 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>
    

    【讨论】:

    • 我的问题是试图让多个分页工作。
    【解决方案2】:

    实际上,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)
    

    【讨论】:

      猜你喜欢
      • 2013-08-03
      • 2018-06-02
      • 1970-01-01
      • 2013-01-18
      • 2014-07-23
      • 1970-01-01
      • 2019-02-11
      • 2019-04-11
      • 2023-03-15
      相关资源
      最近更新 更多