【发布时间】:2021-09-22 06:10:39
【问题描述】:
我正在尝试通过 ListView 使用 Django Paginator,但分页器显示的是帖子标题而不是页码: 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-->
谢谢!
【问题讨论】:
标签: django django-templates django-pagination