问题描述
我在模板中使用了django paginator。它的工作确定,但不是很好,当有大量的页面。views.py:
def blog(request):
blogs_list = Blog.objects.all()
paginator = Paginator(blogs_list,1)
尝试:
page = int(request.GET.get('page','1'))
除了:
page = 1
try:
blogs = paginator.page(page)
except(EmptyPage,InvalidPage):
blogs = paginator.page(page)
return render(request,'blogs.html',{
'blogs':blogs
})
模板片段: p>
< div class =prev_next>
{%if blogs.has_previous%}
< a class =prev btn btn-infohref =?page = {{blogs.previous_page_number}}> Prev< / A>
{%endif%}
{%if blogs.has_next%}
< a class =next btn btn-infohref =?page = {{blogs.next_page_number}} >接着< / A>
{%endif%}
< div class =pages>
< ul>
{%for pg in blogs.paginator.page_range%}
{%if blogs.number == pg%}
< li>< a href =?page = {{ pg}}class =btn btn-default> {{pg}}< / a>< / li>
{%else%}
< li>< a href =?page = {{pg}}class =btn> {{pg}}< / a> ; /立GT;
{%endif%}
{%endfor%}
< / ul>
< / div>
< span class =clear_both>< / span>
< / div>
现在看起来像这样:
我该怎么办只显示7页数,而不是全部从当前页码开始,如下所示:
上一页1(2)3 4 5下一页
我希望我很清楚,如果不是,请问。您的帮助和指导将非常感谢。谢谢。
首先我会更改以下内容:
try:
blogs = paginator.page(page)
except(EmptyPage,InvalidPage):
blogs = paginator.page(page)#提升相同的错误
但你可以在你的上下文中传递一个范围。
index = paginator.page_range.index(blogs.number)
max_index = len(paginator.page_range)
start_index =索引 - 3 if index> = 3 else 0
end_index = index + 3 if index< = max_index - 3 else max_index
page_range = paginator.page_range [start_index:end_index]
现在,您应该可以循环使用来构建正确的链接?page =
。
===编辑===
所以你的看法会是这样的:
paginator = Paginator(Blog.objects.all(),1)
try:
page = int(request.GET.get('page','1'))
除了:
page = 1
try:
blogs = paginator。页面(页)
except(EmptyPage,InvalidPage):
blogs = paginator.page(1)
#获取当前页面的索引
index = blogs .number - 1#编辑更容易没有索引
#这个值是你的页面的最大索引,所以最后一页 - 1
max_index = len(paginator.page_range)
#你想要3,如果index< = max_index - 3 else max_index $,那么我们可以计算在哪里切片列表
start_index = index - 3 if index> = 3 else 0
end_index = b $ b#我的新页面范围
page_range = paginator.page_range [start_index:end_index]
return render(request,'blogs.html',{
'blogs':blogs,
'page_range':page_range,
})
所以现在我们必须编辑你的模板来接受我们的新的页码列表:
< div class =prev_next>
{%if blogs.has_previous%}
< a class =prev btn btn-infohref =?page = {{blogs.previous_page_number}}> Prev< / a>
{%endif%}
{%if blogs.has_next%}
< a class =next btn btn-infohref =?page = {{blogs.next_page_number}} >接着< / A>
{%endif%}
< div class =pages>
< ul>
{%for page_range%}
{%if blogs.number == pg%}
< li>< a href =?page = {{pg}} class =btn btn-default> {{pg}}< / a>< / li>
{%else%}
< li>< a href =?page = {{pg}}class =btn> {{pg}}< / a> ; /立GT;
{%endif%}
{%endfor%}
< / ul>
< / div>
< span class =clear_both>< / span>
< / div>
I am using the django paginator in the template. Its working ok, but not good when there's large numbers of pages.
views.py:
def blog(request):
blogs_list = Blog.objects.all()
paginator = Paginator(blogs_list, 1)
try:
page = int(request.GET.get('page', '1'))
except:
page = 1
try:
blogs = paginator.page(page)
except(EmptyPage, InvalidPage):
blogs = paginator.page(page)
return render(request, 'blogs.html', {
'blogs':blogs
})
snippet of the template:
<div class="prev_next">
{% if blogs.has_previous %}
<a class="prev btn btn-info" href="?page={{blogs.previous_page_number}}">Prev</a>
{% endif %}
{% if blogs.has_next %}
<a class="next btn btn-info" href="?page={{blogs.next_page_number}}">Next</a>
{% endif %}
<div class="pages">
<ul>
{% for pg in blogs.paginator.page_range %}
{% if blogs.number == pg %}
<li><a href="?page={{pg}}" class="btn btn-default">{{pg}}</a></li>
{% else %}
<li><a href="?page={{pg}}" class="btn">{{pg}}</a></li>
{% endif %}
{% endfor %}
</ul>
</div>
<span class="clear_both"></span>
</div>
Now it looks like this:
What do I do to display only 7 page numbers and not all of it ranging from the current page number, like this:
Prev 1 (2) 3 4 5 Next
I hope I was clear, if not please ask. Your help and guidance will be very much appreciated. Thank you.
First of all I would change the following:
try:
blogs = paginator.page(page)
except(EmptyPage, InvalidPage):
blogs = paginator.page(page) # Raises the same error
But you could pass a range within your context.
index = paginator.page_range.index(blogs.number)
max_index = len(paginator.page_range)
start_index = index - 3 if index >= 3 else 0
end_index = index + 3 if index <= max_index - 3 else max_index
page_range = paginator.page_range[start_index:end_index]
Now you should be able to loop over the range to construct the right links with ?page=
.
=== Edit ===
So your view would be something like this:
def blog(request):
paginator = Paginator(Blog.objects.all(), 1)
try:
page = int(request.GET.get('page', '1'))
except:
page = 1
try:
blogs = paginator.page(page)
except(EmptyPage, InvalidPage):
blogs = paginator.page(1)
# Get the index of the current page
index = blogs.number - 1 # edited to something easier without index
# This value is maximum index of your pages, so the last page - 1
max_index = len(paginator.page_range)
# You want a range of 7, so lets calculate where to slice the list
start_index = index - 3 if index >= 3 else 0
end_index = index + 3 if index <= max_index - 3 else max_index
# My new page range
page_range = paginator.page_range[start_index:end_index]
return render(request, 'blogs.html', {
'blogs': blogs,
'page_range': page_range,
})
So now we have to edit your template to accept our new list of page numbers:
<div class="prev_next">
{% if blogs.has_previous %}
<a class="prev btn btn-info" href="?page={{blogs.previous_page_number}}">Prev</a>
{% endif %}
{% if blogs.has_next %}
<a class="next btn btn-info" href="?page={{blogs.next_page_number}}">Next</a>
{% endif %}
<div class="pages">
<ul>
{% for pg in page_range %}
{% if blogs.number == pg %}
<li><a href="?page={{pg}}" class="btn btn-default">{{pg}}</a></li>
{% else %}
<li><a href="?page={{pg}}" class="btn">{{pg}}</a></li>
{% endif %}
{% endfor %}
</ul>
</div>
<span class="clear_both"></span>
</div>
这篇关于只显示一些页码由django分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!