我在将其添加到模板时使用进行了分页,但由于某些原因,在分页显示的页面上没有显示任何内容
我不知道是否要为此创建一个模型
我的观点
class ArticleIndex(ListView):
model = Articles
template_name = 'news/posts.html'
def get_queryset(self):
articles = Articles.objects.all()
# Отбираем первые 10 статей
paginator = Paginator(articles, 10)
page = self.request.GET.get('page')
try:
articles = paginator.page(page)
except PageNotAnInteger:
# If page is not an integer, deliver first page.
articles = paginator.page(1)
except EmptyPage:
# If page is out of range (e.g. 9999), deliver last page of results.
articles = paginator.page(paginator.num_pages)
return articles
posts.html
<div class="pagination">
{% if articles.has_previous %}
<a href="?page={{ articles.previous_page_number }}">←</a>
{% endif %}
{% for num in articles.paginator.page_range %}
{% if num == articles.number %}
<span class="current"><b>{{ num }}</b></span>
{% else %}
<a href="?page={{ num }}"> {{ num }}</a>
{% endif %}
{% endfor %}
{% if article_list.has_next %}
<a href="?page={{ article_list.next_page_number }}">→</a>
{% endif %}
</div>
目前没有回答
相关问题 更多 >
编程相关推荐