更改每页的项目数或使用 django 中的分页器查看视图中的所有项目

发布于 2024-11-25 11:29:38 字数 832 浏览 0 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一紙繁鸢 2024-12-02 11:29:38

您想要做的是将一个可选参数添加到名为“page_size”的视图中,然后执行以下操作:

def your_view(request, ..., page_size=50):
  scanned_assets_qs = # get your scanned_assets QuerySet

  if page_size == -1:
    page_size = scanned_assets_qs.count()

  paginator = Paginator(scanned_assets_qs, 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:

def your_view(request, ..., page_size=50):
  scanned_assets_qs = # get your scanned_assets QuerySet

  if page_size == -1:
    page_size = scanned_assets_qs.count()

  paginator = Paginator(scanned_assets_qs, page_size)

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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文