2

我正在尝试使用django-endless-pagination在滚动上实现连续分页。

页面的初始渲染工作正常。然而,一旦滚动,整个html 页面内容将被加载到 never_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

my/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>

my/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>

谢谢!

4

1 回答 1

5

我遇到了同样的问题,并通过在 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/render_to_response 而不是返回默认的 get 方法,这取决于您的页面结构。

于 2014-05-17T23:33:14.910 回答