Django:在第一页上分页不同 [英] Django: paginating differently on the first page

查看:148
本文介绍了Django:在第一页上分页不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这一刻,我正在使用Django的object_list来处理分页。如果您在听到我的问题后觉得我需要它,我很乐意转到正确的Paginator()类:



在主页上,我想分页 7 ,但在所有其他页面中,我想通过 10 进行分页。



我会做这个吗我真的不能让我的头围绕它。最近我要做的工作导致一整页的结果被遗漏了,所以显然我不想要。



我会非常赞赏任何答案。如果您需要更多信息,请告诉我们。非常感谢。



Joe

解决方案

Just 从django.core.paginator import Paginator ,并在页面视图中创建一个paginator对象作为 p = Paginator(thestuff,7) p = Paginator(the stuff,10)其他地方。那么在这两种情况下,在用于渲染模板的上下文中绑定 p 。在任何一种情况下都会适当设置 p.object_list (您似乎认为这是您正在使用的方法,对吗?就是这样,你的意思是Django的object_list ?)。



Django docs 具有优秀的细节和例子(假设你是1.0或更好)。如果您无法使其正常工作,可以向我们显示(简化版本,仍然失败)您的模板和查看代码?



编辑:问题现在已经清楚地显示,我认为应该通过对Django的核心分页进行子类化来解决,如下所示:

  from django.core.paginator import Paginator,页

class MyPaginator(Paginator):

def __init __(self,** kw):
self.deltafirst = kw.pop('deltafirst',0 )
Paginator .__ init __(self,** kw)

def page(self,number):
返回给定基于1的页码的Page对象。
number = self.validate_number(number)
如果number == 1:
bottom = 0
top = self.per_page - self.deltafirst
else:
bottom =(number - 1)* self.per_page - self.deltafirst
top = bottom + self.per_page
如果顶部+ self.orphans> = self.count:
顶部= self.count
返回页面(self.object_list [bottom:top],number,self)

现在使用MyPaginator完全如上面的文本,示例显示了Django自己的用法,除了创建它之外,使用额外的命名参数 deltafirst = 3 第一页3比正常每页长度(10)短。因此,您将使用一个名义长度为10的单个分页符,但是三分之一为3,使第一页比所有其他页面短3。



validate_number 中的问题,但我不确定他们会出现 - 如果他们这样做,那么MyPaginator也需要覆盖该方法)。


At the minute, I'm using Django's object_list to handle pagination. I'm happy to move to the proper Paginator() class if you think I need it after you hear my problem:

On the homepage, I want to paginate by 7, but on all other pages I want to paginate by 10.

How would I go about doing this? I really can't get my head around it. The closest I've got to making it work resulted in a whole page of results being left out, so obviously I don't want that.

I'd be extremely appreciative of any answers. Let me know if you need any more information. Thanks a lot.

Joe

解决方案

Just from django.core.paginator import Paginator and make a paginator object as p = Paginator(thestuff, 7) in the view for the homepage, p = Paginator(thestuff, 10) everywhere else. Then in either case bind p in the context you use to render the template. p.object_list will be set appropriately in either case (and you appear to say that's the approach you're using, right? I.e., is that what you mean by "Django's object_list"?).

Django docs have excellent details and examples (assuming you're on 1.0 or better). If you can't make it work, can you show us (a simplified version that still fails, of) your template and view code?

Edit: problem has now been clearly shown and I think should be solved by subclassing Django's core paginator, as follows:

from django.core.paginator import Paginator, Page

class MyPaginator(Paginator):

  def __init__(self, **kw):
    self.deltafirst = kw.pop('deltafirst', 0)
    Paginator.__init__(self, **kw)

  def page(self, number):
    "Returns a Page object for the given 1-based page number."
    number = self.validate_number(number)
    if number == 1:
      bottom = 0
      top = self.per_page - self.deltafirst
    else:
      bottom = (number - 1) * self.per_page - self.deltafirst
      top = bottom + self.per_page
    if top + self.orphans >= self.count:
      top = self.count
    return Page(self.object_list[bottom:top], number, self)

Now use MyPaginator exactly as the above text and examples show the usage of Django's own, except, on creating it, use an extra named argument deltafirst=3 to make the first page 3 shorter than the normal per-page length (of 10). So you'd be using a single paginator with a nominal length of 10 but a deltafirst of 3 to make the first page 3 shorter than all others.

(There may be problems in validate_number but I'm not sure they'd appear -- if they do, then MyPaginator will need to override that method as well).

这篇关于Django:在第一页上分页不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆