ListView的分页 - Django
Pagination of ListView - Django
观看次数
class ThreadListView(ListView):
model = Thread
template_name = 'forums/thread.html'
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'] = Thread.objects.get(pk=self.kwargs['pk'])
context['reply'] = Thread.objects.get(pk=self.kwargs['pk']).replies.all()
return context
HTML
{% 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 %}
我想对 Thread ListView 进行分页。
Thread ListView 显示 Thread,然后显示该线程上的回复。
我希望能够将所有内容拆分成页面。
例如,话题以话题 post 和 10 个回复开头,然后要查看一些较新的回复,您可以单击下一页。
将其设为 Replies
的 ListView
并简单地使用 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>
观看次数
class ThreadListView(ListView):
model = Thread
template_name = 'forums/thread.html'
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'] = Thread.objects.get(pk=self.kwargs['pk'])
context['reply'] = Thread.objects.get(pk=self.kwargs['pk']).replies.all()
return context
HTML
{% 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 %}
我想对 Thread ListView 进行分页。
Thread ListView 显示 Thread,然后显示该线程上的回复。
我希望能够将所有内容拆分成页面。
例如,话题以话题 post 和 10 个回复开头,然后要查看一些较新的回复,您可以单击下一页。
将其设为 Replies
的 ListView
并简单地使用 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>