分页问题

发布于 2024-08-13 03:14:47 字数 1403 浏览 3 评论 0原文

我正在使用此代码进行分页,并且我希望用户的选择在整个站点中保持不变(到目前为止已解决)...现在唯一的问题是会话变量现在是永久的,直到会话结束关闭浏览器即可清除。另外,我怎样才能显示相邻页面...就像在 digg 风格的 Django 分页器中一样。我无法理解如何将其实现到我的代码中。

代码如下:

from django.core.paginator import Paginator, InvalidPage, EmptyPage

def paginate(request, object_list, paginate_by=10):
   try:
      if "per_page" in request.session:
        per_page = request.session["per_page"]
      else:
        request.session["per_page"] = int(request.REQUEST['p'])
        per_page = request.session["per_page"]
        request.session.set_expiry(0)
   except:
      per_page = 10

   paginator = Paginator(object_list, per_page)

   try:
      page = int(request.GET.get('page', '1'))
   except ValueError:
      page = 1

   try:
      items = paginator.page(page)
   except (EmptyPage, InvalidPage):
      items = paginator.page(paginator.num_pages)

   return items

然后在我的模板中,我用它来呈现分页链接:

<div class="pagination" align="center"> 
 <span class="step-links"> 
  {% if items.has_previous %} 
    <a href="?page={{ items.previous_page_number }}">previous</a> 
  {% endif %} 
  <span class="current"> 
    Page {{ items.number }} of {{ items.paginator.num_pages }} 
  </span> 
  {% if items.has_next %} 
    <a href="?page={{ items.next_page_number }}">next</a> 
  {% endif %} 
 </span> 
</div>

I'm using this code for my pagination, and I'd like the user's choice to be persistent throughout the site (this has been solved so far)...the only problem now is that the session variable now is permanent until the session is cleared by closing the browser. Also, how can I get the adjacent pages displayed...like in the digg-style Django paginator. I haven't been able to make sense of how to implement this into my code.

The code is as follows:

from django.core.paginator import Paginator, InvalidPage, EmptyPage

def paginate(request, object_list, paginate_by=10):
   try:
      if "per_page" in request.session:
        per_page = request.session["per_page"]
      else:
        request.session["per_page"] = int(request.REQUEST['p'])
        per_page = request.session["per_page"]
        request.session.set_expiry(0)
   except:
      per_page = 10

   paginator = Paginator(object_list, per_page)

   try:
      page = int(request.GET.get('page', '1'))
   except ValueError:
      page = 1

   try:
      items = paginator.page(page)
   except (EmptyPage, InvalidPage):
      items = paginator.page(paginator.num_pages)

   return items

Then in my template I have this to render the pagination links:

<div class="pagination" align="center"> 
 <span class="step-links"> 
  {% if items.has_previous %} 
    <a href="?page={{ items.previous_page_number }}">previous</a> 
  {% endif %} 
  <span class="current"> 
    Page {{ items.number }} of {{ items.paginator.num_pages }} 
  </span> 
  {% if items.has_next %} 
    <a href="?page={{ items.next_page_number }}">next</a> 
  {% endif %} 
 </span> 
</div>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

自在安然 2024-08-20 03:14:47

您可以通过启用会话来实现此目的。

我建议阅读 djangobook 网站上的会话、用户和注册一章。


编辑:现在您已经启用了会话,我认为问题出在模板中的超链接。使用 & 分隔 url 中的多个参数,例如

 <a href="?p={{ request.session.per_page }}&page={{ items.next_page_number }}">next</a> 

编辑 2:我不确定我是否理解会话过期的问题所在。将会话设置为在浏览器关闭时过期的行是 request.session.set_expiry(0)。请参阅 django 文档 在视图中使用会话 如果你想改变它。

要制作 Digg 风格的分页器,您需要编写一个函数,该函数获取当前页码和总页数,并返回页码列表。然后,在模板中循环访问页码并构建页面链接。

页码列表允许您将页码分成组,例如

[[1,2], [20,21,22,23,24], [30,31]]

You can achieve this by enabling sessions.

I recommend reading through the chapter Sessions, Users, and Registration on the djangobook website.


Edit: Now that you've enabled sessions, I think the problem is the hyperlinks in the template. Use an ampersand to separate multiple parameters in a url, for example

 <a href="?p={{ request.session.per_page }}&page={{ items.next_page_number }}">next</a> 

Edit 2: I'm not sure if I understand what the problem is with the session expiry. The line that sets the session to expire when the browser closes is request.session.set_expiry(0). See the django docs on Using Sessions in views if you want to change that.

To make a Digg style paginator, you need to write a function that takes the current page number and the total number of pages, and returns a list of page numbers. Then, in the template, loop through the page numbers and construct links to the pages.

A list of lists of page numbers would allow you to split the page numbers into groups, eg

[[1,2], [20,21,22,23,24], [30,31]]

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