更改每页的项目数或使用 django 中的分页器查看视图中的所有项目
我正在使用 django 1.3,并且我有一个采用分页查询集的视图(设置为使用 50 个对象)。以下是视图中 get_context_data() 方法的相关部分:
#build paginator for querysets
paginated_scanned_assets = Paginator(scanned_assets_qs, 50)
#get the specified page
try:
page_num = int(self.request.GET.get('page', '1'))
except ValueError:
page_num = 1
#get a paginated list of object
try:
scanned_assets = paginated_scanned_assets.page(page_num)
except (EmptyPage, InvalidPage):
scanned_assets = paginated_scanned_assets.page(paginated_scanned_assets.num_pages)
它呈现的模板仅根据查询集构建一个表,并具有转到下一页和上一页的链接。
我想要做的是要么有一个查看全部的链接,这只会显示未分页的查询集,要么有一个修改每页对象数量的选项(这将重新创建分页器)。不过,我没有太多使用视图或设计的经验,所以我不确定如何做到这一点。我可以用js做一些事情吗?如果可以的话怎么做?否则我可以用 django 和 html 来做吗?这可能非常简单,我只是非常缺乏经验,在任何地方都没有找到答案或示例。
谢谢!
I am using django 1.3 and I have a view that takes a paginated queryset (set to use 50 objects). Here is the relevant part of the get_context_data() method in the view:
#build paginator for querysets
paginated_scanned_assets = Paginator(scanned_assets_qs, 50)
#get the specified page
try:
page_num = int(self.request.GET.get('page', '1'))
except ValueError:
page_num = 1
#get a paginated list of object
try:
scanned_assets = paginated_scanned_assets.page(page_num)
except (EmptyPage, InvalidPage):
scanned_assets = paginated_scanned_assets.page(paginated_scanned_assets.num_pages)
The template it renders to just builds a table from the queryset and has links to go to the next and previous pages.
What I want to do is either have a link to view all, which would just display the queryset unpaginated, or an option to modify the number of objects per page (which would recreate the paginator). I haven't worked with views or design much though so I am not sure how to do this. Is it something I can do with js, and if so how? Otherwise can I just do it with django and html? It might be really simple, I am just pretty inexperienced and haven't found an answer or example anywhere.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您想要做的是将一个可选参数添加到名为“page_size”的视图中,然后执行以下操作:
然后您可以传入任意 page_size,并为查看所有页面传递 -1(我会使用适当的 url 来做到这一点) ,加上 /viewall/ 的 url,无论如何都会传入 -1)
What you want to do is add an optional parameter to the view called 'page_size' and then do something like:
Then you can pass in an arbitrary page_size, and a -1 for a view all page (I would do that with the appropriate urls, plus a url for /viewall/ that passes in -1 no matter what)