2013-12-10 236 views
3

我想在滚动上使用django-endless-pagination实现连续分页。无尽的分页加载滚动的整个页面内容

该页面的初始渲染正常工作。但是,一旦滚动,html页面内容将被加载到endless_page_template div中,而不是来自page_template的所需部分html内容。结果就像是看着一面镜子反射着背后的另一面镜子。我相信返回的查询集是正确的,因为在不尝试使用“paginateOnScroll”时分页结果是正确的。

我的观点的相关部分如下。我使用的是CreateView,因为我在同一页面上有评论表单和分页评论。

class MyIndex(CreateView): 
    form_class = CommentForm 
    template_name = 'my/index.html' 
    page_template = 'my/comments.html' 

    def get_context_data(self, **kwargs): 
     context = super(MyIndex, self).get_context_data(**kwargs) 
     context.update({ 
      'comments': Comment.objects.order_by('-id').filter(parent=None), 
      'page_template': self.page_template, 
     }) 

     return context 

我/ index.html的模板的相关部分(主模板)

<script src="{% static 'endless_pagination/js/endless-pagination.js' %}"></script> 
<script type="text/javascript"> 
    $.endlessPaginate({ 
     paginateOnScroll: true 
    }); 
</script> 

<div class="endless_page_template"> 
    {% include page_template %} 
</div> 

我/ comments.html的相关部分(page_template)

{% load endless %} 

{% paginate comments %} 
{% for comment in comments %} 
    <span class="lead">{{ comment.name }}</span> 
    {{ comment.message}} 

    {% if not forloop.last %} 
     <hr /> 
    {% endif %} 
{% endfor %} 
<br /> 

<div class="row-fluid"> 
    <div class="span8 offset2 pagination-centered"> 
     {% show_more %} 
    </div> 
</div> 

谢谢!

+0

TBH,从你的代码中我们可以看到关于分页的任何内容。如果这确实是django和分页问题,​​那么首先应该包含您的视图部分,该部分涉及分页。在这篇文章中,我们没有看到:1)分页,2)在JavaScript中,你指的是哪个页面或哪些对象应该包含在下一页/批处理中。你在模板中也有show_more标签,但它有什么作用? –

+0

@Odif Yltsaeb,谢谢你评论我的问题。我意识到我的错误,并通过澄清我正在使用django-endless-pagination应用程序更新了我的帖子。谢谢。 – pymarco

+0

那么,如果这是你可以给的所有信息,那么我会说,问题是在你的查询中,你返回的所有对象没有分页的对象列表.... –

回答

4

我有同样的问题,并明确将在views.py文件中is_ajax检查,例如固定它:

class MyIndex(CreateView): 
    form_class = CommentForm 
    template_name = 'my/index.html' 
    page_template = 'my/comments.html' 

    def get(self, request, *args, **kwargs): 
     if request.is_ajax(): 
      self.template_name = self.page_template 
     return super(MyIndex, self).get(request, *args, **kwargs) 

    def get_context_data(self, **kwargs): 
     context = super(MyIndex, self).get_context_data(**kwargs) 
     context.update({ 
      'comments': Comment.objects.order_by('-id').filter(parent=None), 
      'page_template': self.page_template, 
     }) 

     return context 

您可能还需要使用渲染/选择render_to_response而不是返回默认尽管取得方法,这取决于你的页面的结构。