Endless pagination loads entire page contents on s

2019-07-08 20:19发布

问题:

I am trying to implement continuous pagination on scroll using django-endless-pagination.

The initial rendering of the page works fine. Once scrolled, however, the entire html page contents are loaded into the endless_page_template div, rather than the desired partial html content from the page_template. The result is sort of like looking into a mirror that is reflecting another mirror behind it. I believe that the queryset returned is correct because the pagination results are correct when not attempting to use "paginateOnScroll".

The relevant parts of my view are below. I'm using a CreateView because I have the comments form on the same page as the paginated comments.

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

The relevant parts of my/index.html template (main template)

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

The relevant parts of 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>

Thanks!

回答1:

I had the same issue, and fixed it by explicitly adding an is_ajax check in the views.py file, for example:

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

You also might need to use render/render_to_response rather than returning the default get method though, it depends on how your page is structured.