1 回答

TA贡献1884条经验 获得超4个赞
将其设为ListViewforReplies并简单地使用paginate_by.
class ThreadListView(ListView):
model = Reply #change the model
template_name = 'forums/thread.html'
paginate_by = 10 # add pagination on the list
def get_queryset(self):
self.thread = Thread.objects.get(pk=self.kwargs['pk'])
return self.thread.replies.all().order_by('date_posted')
def get_context_data(self, **kwargs):
# Call the base implementation first to get a context
context = super().get_context_data(**kwargs)
# Add in a QuerySet of the Thread & Replies
context['thread'] = self.thread
return context
然后,在您的模板中,添加分页代码。使用object_list遍历回复
...
<!--Reply Author and Content-->
{% for rply in object_list %}
....
<div class="container">
{% if is_paginated %}
{% if page_obj.has_other_pages %}
<ul class="pagination justify-content-center" style="margin:20px 0">
{% if page_obj.has_previous %}
<li class="page-item"><a class="page-link" href="?page={{ page_obj.previous_page_number }}">Previous</a></li>
{% else %}
<li class="page-item disabled"><a class="page-link" href="#">Previous</a></li>
{% endif %}
{% for i in page_obj.paginator.page_range %}
{% if page_obj.number == i %}
<li class="page-item active"><a class="page-link" href="?page={{ i }}">{{ i }}</a></li>
{% else %}
<li class="page-item"><a class="page-link" href="?page={{ i }}">{{ i }}</a></li>
{% endif %}
{% endfor %}
{% if page_obj.has_next %}
<li class="page-item"><a class="page-link" href="?page={{ page_obj.next_page_number }}">Next</a></li>
{% else %}
<li class="page-item disabled"><a class="page-link" href="#">Next</a></li>
{% endif %}
</ul>
{% endif %}
{% endif %}
</div>
添加回答
举报