Python ListView的分页-Django 意见 HTML {%extends'论坛/base.html%} {%block title%}论坛-{{thread.title}{%endblock title%} {%block content%} 用户名:&emsp 职级:  职位:  徽章:  加入日期: {{thread.author.date|u joined|date:'Y-m-d'}} {{thread.content}} {答复中rply的百分比为%} 用户名:&emsp 职级:  职位:  徽章:  加入日期: {{thread.author.date|u joined|date:'Y-m-d'}}
{{rply.content} {%endfor%} {%endblock内容%}Python ListView的分页-Django 意见 HTML {%extends'论坛/base.html%} {%block title%}论坛-{{thread.title}{%endblock title%} {%block content%} 用户名:&emsp 职级:  职位:  徽章:  加入日期: {{thread.author.date|u joined|date:'Y-m-d'}} {{thread.content}} {答复中rply的百分比为%} 用户名:&emsp 职级:  职位:  徽章:  加入日期: {{thread.author.date|u joined|date:'Y-m-d'}},python,django,django-pagination,Python,Django,Django Pagination,{{rply.content} {%endfor%} {%endblock内容%} 我想为线程列表视图分页 线程列表视图显示线程,然后显示该线程上的回复 我希望能够将所有内容拆分为页面 例如,该线程从线程帖子和10条回复开始,然后查看一些较新的回复,您可以单击下一页。将其设置为回复的列表视图,只需使用分页即可 {% extends 'forums/base.html' %} {% block title %} Forum - {{ thread.title }} {% endblock t
我想为线程列表视图分页 线程列表视图显示线程,然后显示该线程上的回复 我希望能够将所有内容拆分为页面
例如,该线程从线程帖子和10条回复开始,然后查看一些较新的回复,您可以单击下一页。将其设置为
回复的列表视图,只需使用分页即可
{% extends 'forums/base.html' %}
{% block title %} Forum - {{ thread.title }} {% endblock title %}
{% block content %}
<!--Thread real-->
<table class="table table-hover">
<thead>
<tr class="table-primary">
<th class="col-2"><a href="{% url 'threadview' thread.id %}"> {{ thread.title }}</a></th>
<th scope="col-10" id="content-col"></th>
</tr>
</thead>
<tbody>
<!--Thread Author and Content-->
<tr class="table-info">
<td class="border-right text-center">
<span>
<img class="rounded-circle" style="height: 100px;width: 100px;"
src="{{ thread.author.profile.image.url }}"> <br />
Username: <a href="#">{{ thread.author.username|capfirst }}</a> <br />
Ranks: 
<!--Ranks Go Here--> <br />
<hr>
Posts: 
<!--Posts Go Here--> <br />
Badges: 
<!--Badges Go Here--> <br />
<hr>
Date Joined: {{thread.author.date_joined| date:'Y-m-d'}} <br />
</span>
</td>
<td>{{ thread.content }}</td>
</tr>
<!--Reply Author and Content-->
{% for rply in reply %}
<tr class="table-secondary">
<td class="border-right text-center">
<span>
<img class="rounded-circle" style="height: 100px;width: 100px;"
src="{{ rply.author.profile.image.url }}"> <br />
Username: <a href="#">{{ rply.author.username|capfirst }}</a> <br />
Ranks: 
<!--Ranks Go Here--> <br />
<hr>
Posts: 
<!--Posts Go Here--> <br />
Badges: 
<!--Badges Go Here--> <br />
<hr>
Date Joined: {{thread.author.date_joined| date:'Y-m-d'}} <br />
</span>
</td>
<td>
<p>{{ rply.content }}</p>
</td>
</tr>
{% endfor %}
</tbody>
</table>
{% endblock content %}
然后,在模板中添加分页代码。使用object\u list
迭代回复
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
。。。
{对象中rply的%u列表%}
....
{%if已分页%}
{%如果页面对象有其他页面%}
{%如果页面_obj.has_previous%}
{%else%}
{%endif%}
{page_obj.paginator.page_range%}
{%如果页码=i%}
{%else%}
{%endif%}
{%endfor%}
{%如果页面_obj.has_next%}
{%else%}
{%endif%}
{%endif%}
{%endif%}
刚刚试过。它显示分页链接。但是当页面改变时,它不会改变内容。我将paginate_by更改为=3,因为我有4个回复设置和一个线程。但是当我在第一页或第二页时,它会显示所有的四个回复。。使用object\u list
对回复进行迭代,感谢它能正常工作。我不得不在回复的末尾添加排序。all()。我要编辑你的答案,把它加进去。
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
...
<!--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>