由于某些原因,我的分页器在我的post_list页面上无法正常工作,但它适用于我的tags_list_page并且几乎相同。它不会显示页数。继承我的代码
发布list.html分页
<div class="text-center" style="margin-bottom: 20px">
<ul class="pagination">
{% if object_list.has_previous %}
<li><a href="?page=1"><<</a></li>
<li><a href="?{{ page_request_var }}={{ object_list.previous_page_number }}
{% if request.GET.q %}&q={{ request.GET.q }}{% endif %}">prev</a></li>
{% endif %}
{% for i in paginator.page_range %}
<li {% if page_obj.number == i %} class="active" {% endif %}><a href="?page={{i}}">{{i}}</a><li>
{% endfor %}
{% if object_list.has_next %}
<li><a href="?{{ page_request_var }}={{ object_list.next_page_number }}
{% if request.GET.q %}&q={{ request.GET.q }}{% endif %}">next</a></li>
<li><a href="?page={{ object_list.paginator.num_pages }}">>></a></li>
{% endif %}
</ul>
标记list.html分页
<div class="text-center" style="margin-bottom: 20px">
<ul class="pagination">
{% if queryset.has_previous %}
<li><a href="?tags=1"><<</a></li>
<li><a href="?{{ page_request_var }}={{ queryset.previous_page_number }}
{% if request.GET.q %}&q={{ request.GET.q }}{% endif %}">prev</a></li>
{% endif %}
{% for i in paginator.page_range %}
<li {% if page_obj.number == i %} class="active" {% endif %}><a href="?tags={{i}}">{{i}}</a><li>
{% endfor %}
{% if queryset.has_next %}
<li><a href="?{{ page_request_var }}={{ queryset.next_page_number }}
{% if request.GET.q %}&q={{ request.GET.q }}{% endif %}">next</a></li>
<li><a href="?tags={{ queryset.paginator.num_pages }}">>></a></li>
{% endif %}
</ul>
我的观点
def post_list(request):
today = timezone.now().date()
queryset_list = Post.objects.active()
if request.user.is_staff or request.user.is_superuser:
queryset_list = Post.objects.all()
paginator = Paginator(queryset_list, 8)
page_request_var = 'page'
page = request.GET.get(page_request_var)
try:
queryset = paginator.page(page)
except PageNotAnInteger:
queryset = paginator.page(1)
except EmptyPage:
queryset = paginator.page(paginator.num_pages)
template = "posts/post_list.html"
name = "user"
count = queryset_list.count()
context = {
"object_list": queryset,
"name": name,
"page_request_var": page_request_var,
"today": today,
"count": count
}
return render(request, template, context)
def tag_list(request, slug=None):
today = timezone.now().date()
instance = get_object_or_404(Tag, slug=slug)
ins = instance.post_set.all()
queryset_list = Post.objects.active()
if request.user.is_staff or request.user.is_superuser:
queryset_list = ins
paginator = Paginator(queryset_list, 9)
page_request_var = "tags"
page = request.GET.get(page_request_var)
try:
queryset = paginator.page(page)
except PageNotAnInteger:
queryset = paginator.page(1)
except EmptyPage:
queryset = paginator.page(paginator.num_pages)
hey = paginator.num_pages
kount = queryset_list.count()
name = "Tags list"
context = {
"queryset": queryset,
"paginator": paginator,
"page_request_var": page_request_var,
"hey": hey,
"title": "posts",
"name": name,
"today": today,
"kount": kount
}
return render(request, "posts/tag_list.html", context)
正如我所说,它适用于标签分页。不知道发生了什么
It was working fine. Any help is appreciated
答案 0 :(得分:1)
您没有将分页器传递到post_list
视图中的模板。