Django 分页 |获取页面索引中分页项目的当前索引(不是页面索引范围本身)
我正在尝试用 Django 建立一个照片库。
它是按类别设置的。
我已经按每页 n 个图像数量对一个类别的结果进行了分页。我还想在页面上使用分页器,仅显示单个图像,并为该类别中的上一张/下一张图像提供上一张/下一张按钮。
我的想法是获取图像本身的当前索引,并将其作为指向 /category/CUR_IMG_ID_PAGINATION_LIST/ 的链接,因为对整个集合进行分页的结果将产生与分页结果中当前图像索引相同的索引。
例如,如果我想要的图像是某个类别总共 150 张图像中的第 45 张图像,那么当我对 150 张图像进行分页时,45 将是我想要的实际页码。
如果有更简单的方法可以做到这一点,请告诉我。姜戈1.1
I am trying to build a photo gallery with Django.
It is set up by category.
I have paginated the results of a category by n amount of images per page. I want to also use the paginator on the page that shows just the single image and have a prev/next button for the prev/next image in that category.
My thought was to get the current index for the image itself and have that be the link to the /category/CUR_IMG_ID_PAGINATION_LIST/ as the result of paginating the entire set would yield the same index as the current image index in the paginated results.
For instance if the image i want is image 45 out of 150 images total for a category, then when i paginate the 150 images the 45 will be the actual number of the page I want.
If there's an easier way to do this, let me know. Django 1.1
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为你描述它的方式可以正常工作,因为在幕后我相信 Django 正在做的是使用 SQL
LIMIT
来简单地让数据库完成分类什么和多少的繁重工作要返回的数据。因为数据库针对执行此类操作进行了优化,所以它可能是执行此操作的合理方法。关键可能是保持查询相同,正如您所演示的,您可以使用相同的视图来执行此操作。该视图可以简单地具有一种模式,这是更改分页页数的一种奇特方式。
您最终可能会得到这样的网址...
渲染模板时,分页器将提供
has_next
和has_previous
方法,让您知道是否可以使用渲染 Next /上一个链接。这就是我对视图的想法,或者类似的东西(这完全未经测试并且是从我的脑海中写下来的)......
I think the way you're describing it would work ok because behind the scenes I believe what Django is doing is using an SQL
LIMIT
to simply let the database do the heavy lifting of sorting out what and how much data to return. Because the database is optimized for doing this type of thing it's probably a reasonable way to perform this.The key will probably be to keep the query the same and as you've demonstrated you could use the same view to do that. The view could simply have a mode which is a fancy way of changing the pagination page count.
You could end up with urls like this...
When the template is rendered, the paginator will offer the
has_next
andhas_previous
methods letting you know if you can use render a Next/Previous link.Here's what I'm thinking for the view, or something along these lines (this is totally un-tested and written off the top of my head)...