Django Paginator 显示 post 个标题而不是页码,为什么?
Django Paginator shows post titles Not Page numbers, why?
我正在尝试通过 ListView 使用 Django 分页器,但分页器显示 post 标题而不是页码:
Page number replaced with post title
我该如何解决?
这是我的代码:
在 views.py
:
from django.shortcuts import render
from django.core.paginator import Paginator
from django.views.generic import ListView, DetailView
from .models import Post
# Create your views here.
# PostObject_list by using ListView
class PostList(ListView):
model=Post
paginate_by=20
context_object_name='all_posts'
ordering=['-created_at']
# allpost by using ListView
'''class AllPost(ListView):
model=Post
context_object_name='allpost'
paginate_by=None
'''
# Post_singl_object using DetailView
class PostDetail(DetailView):
model=Post
在模板中 post_list.html
:
</div>
<nav aria-label="Page navigation example">
<ul class="pagination d-flex justify-content-center mt-1">
{% if all_posts.has_previous %}
<li class="page-item "><a class="page-link" href="?page={{ all_posts.previous_page_number }}">Previous</a></li>
{% endif %}
{% for page in all_posts %}
<li class="page-item ">
<a class="page-link" href="?page={{ page }}"><span>{{page}}</span></a>
</li>
{% endfor %}
{% if all_posts.has_next %}
<li class="page-item"><a class="page-link" href="?page={{all_posts.previous_page_number }}">Next</a></li>
{% endif %}
</ul>
</nav>
</div>
<!-- Pagination-->
谢谢!
您应该可以访问 paginator
。在你的例子中,page_obj.paginator
。尝试将您的模板更改为:
{% for i in page_obj.paginator.page_range %}
<li class="page-item ">
<a class="page-link" href="?page={{ page }}"><span>{{page}}</span></a>
</li>
{% endfor %}
使用all_posts
将不起作用,因为这是对象列表,而不是页面对象。 all_posts
不会有paginator
、has_previous
、previous_page_number
等页面属性
在这种情况下,您需要使用 page_obj
,它会在分页时由 ListView
自动添加到模板上下文中。您可以使用 page_obj.paginator.page_range
:
从模板中获取页面范围
{% for page in page_obj.paginator.page_range %}
<li class="page-item ">
<a class="page-link" href="?page={{ page }}"><span>{{ page }}</span></a>
</li>
{% endfor %}
Previous
和 Next
的实施也将因同样的原因而不起作用。要解决这个问题,您还需要使用 page_obj
,所以总而言之:
{% if page_obj.has_previous %}
<li class="page-item "><a class="page-link" href="?page={{ page_obj.previous_page_number }}">Previous</a></li>
{% endif %}
{% for page in page_obj.paginator.page_range %}
<li class="page-item ">
<a class="page-link" href="?page={{ page }}"><span>{{ page }}</span></a>
</li>
{% endfor %}
{% if page_obj.has_next %}
<li class="page-item"><a class="page-link" href="?page={{ page_obj.next_page_number }}">Next</a></li>
{% endif %}
我正在尝试通过 ListView 使用 Django 分页器,但分页器显示 post 标题而不是页码: Page number replaced with post title
我该如何解决?
这是我的代码:
在 views.py
:
from django.shortcuts import render
from django.core.paginator import Paginator
from django.views.generic import ListView, DetailView
from .models import Post
# Create your views here.
# PostObject_list by using ListView
class PostList(ListView):
model=Post
paginate_by=20
context_object_name='all_posts'
ordering=['-created_at']
# allpost by using ListView
'''class AllPost(ListView):
model=Post
context_object_name='allpost'
paginate_by=None
'''
# Post_singl_object using DetailView
class PostDetail(DetailView):
model=Post
在模板中 post_list.html
:
</div>
<nav aria-label="Page navigation example">
<ul class="pagination d-flex justify-content-center mt-1">
{% if all_posts.has_previous %}
<li class="page-item "><a class="page-link" href="?page={{ all_posts.previous_page_number }}">Previous</a></li>
{% endif %}
{% for page in all_posts %}
<li class="page-item ">
<a class="page-link" href="?page={{ page }}"><span>{{page}}</span></a>
</li>
{% endfor %}
{% if all_posts.has_next %}
<li class="page-item"><a class="page-link" href="?page={{all_posts.previous_page_number }}">Next</a></li>
{% endif %}
</ul>
</nav>
</div>
<!-- Pagination-->
谢谢!
您应该可以访问 paginator
。在你的例子中,page_obj.paginator
。尝试将您的模板更改为:
{% for i in page_obj.paginator.page_range %}
<li class="page-item ">
<a class="page-link" href="?page={{ page }}"><span>{{page}}</span></a>
</li>
{% endfor %}
使用all_posts
将不起作用,因为这是对象列表,而不是页面对象。 all_posts
不会有paginator
、has_previous
、previous_page_number
等页面属性
在这种情况下,您需要使用 page_obj
,它会在分页时由 ListView
自动添加到模板上下文中。您可以使用 page_obj.paginator.page_range
:
{% for page in page_obj.paginator.page_range %}
<li class="page-item ">
<a class="page-link" href="?page={{ page }}"><span>{{ page }}</span></a>
</li>
{% endfor %}
Previous
和 Next
的实施也将因同样的原因而不起作用。要解决这个问题,您还需要使用 page_obj
,所以总而言之:
{% if page_obj.has_previous %}
<li class="page-item "><a class="page-link" href="?page={{ page_obj.previous_page_number }}">Previous</a></li>
{% endif %}
{% for page in page_obj.paginator.page_range %}
<li class="page-item ">
<a class="page-link" href="?page={{ page }}"><span>{{ page }}</span></a>
</li>
{% endfor %}
{% if page_obj.has_next %}
<li class="page-item"><a class="page-link" href="?page={{ page_obj.next_page_number }}">Next</a></li>
{% endif %}