简体   繁体   English

Django - Django中的分页1.3

[英]Django - Pagination in Django 1.3

I'm new to Django. 我是Django的新手。 I'm using the simple pagination that Django provides but I need to paginate pages like this: 我正在使用Django提供的简单分页,但我需要对这样的页面进行分页:

Prev 1 2 3 4 5 6 ... 320 上一页1 2 3 4 5 6 ... 320

Or 要么

Prev 120 121 122 123 Last 上一页120 121 122 123最后

There are some code ready to reuse in Django 1.3 to achieve this? 有一些代码可以在Django 1.3中重用以实现这一目的吗?

这就是你要找的。

Let's supose we have a view: 让我们来看看我们有一个观点:

dev showPage(request,pg):

where pg is the page number you are looking at this moment. 其中pg是您目前正在查看的页码。 Then we need a bit of code to get queryset (or objects) and create paginator object: 然后我们需要一些代码来获取queryset(或对象)并创建paginator对象:

pg = int(pg)
objects = range(320)
p = Paginator(objects, 15)
page = p.page(pg)

Well, that you need to get: 好吧,你需要得到:

Prev 1 2 3 4 5 6 ... 320

is send to template a list like pags : 将模板发送到模板,如pags

pags = []   #var to be sent to template
if pg-1 in p.page_range: pags.append( ( 'Prev', p.page( pg - 1) , ) )
for n in range( pg-2, pg+2):
   if n in p.page_range: pags.append( ( n, p.page( pg - 2) , ) )
if p.end_index() not in range( pg-2, pg+2):
   pags.append( ( '...', None , ) )
   pags.append( ( p.end_index(), p.end_index(), ) )

now send pags to your template. 现在将pags发送到您的模板。 And render to something like: 并呈现如下:

<ul>
{% for label, npag in pags %}
   <li>
   {%if npag %} <a href="asjflasdjf/{{npag}"}>{%endif%}
   label
   {%if npag %} </a> {%endif%}
   </li>
{% endfor %}
</ul>

for 对于

Prev 120 121 122 123 124 Last

the solution is the same. 解决方案是一样的。 Decorate a little with css and you get it. 用css装饰一点就可以了。

Also, for example, you can assign a class for current page in order to get it in bold style: 另外,例如,您可以为当前页面指定一个类,以便以粗体显示:

{%if npag %} 
   <a href="asjflasdjf/{{npag}"    
      {% if npag == pg %} class="bold-style" {%endif%}
   }>
{%endif%}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM