Django 分页 |获取页面索引中分页项目的当前索引(不是页面索引范围本身)

发布于 2024-08-27 21:28:39 字数 359 浏览 15 评论 0原文

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

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

发布评论

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

评论(1

调妓 2024-09-03 21:28:39

我认为你描述它的方式可以正常工作,因为在幕后我相信 Django 正在做的是使用 SQL LIMIT 来简单地让数据库完成分类什么和多少的繁重工作要返回的数据。因为数据库针对执行此类操作进行了优化,所以它可能是执行此操作的合理方法。

关键可能是保持查询相同,正如您所演示的,您可以使用相同的视图来执行此操作。该视图可以简单地具有一种模式,这是更改分页页数的一种奇特方式。

您最终可能会得到这样的网址...

# View all "landscape" items in gallery mode starting on page 3
http://www.example.com/gallery/landscape/multi/3

# View the 45th landscape item in singular mode
http://www.example.com/gallery/landscape/single/45

渲染模板时,分页器将提供 has_nexthas_previous 方法,让您知道是否可以使用渲染 Next /上一个链接。

这就是我对视图的想法,或者类似的东西(这完全未经测试并且是从我的脑海中写下来的)......

url(r'gallery/(?P<category>.+)/(?P<mode>.+)/(?P<offset>\d+)
, 'whatever.views.media_gallery'),

def media_gallery(request, category, mode, offset):
    """
    Render a media gallery.
    category = media item category filter
    mode = ( multi | single )
    offset = The pagination offset in multi mode or the media ID in single mode
    """

    if mode == 'multi':
        per_page = 20   # or however many items per page

    elif mode == 'single':
        per_page = 1
    else:
        pass    # handle this however

    # Queryitems
    raw_media_items = Media.objects.filter(category=category)

    # Setup paginator
    paginator = Paginator(raw_media_items, per_page)

    try:
        # in multi mode offset is the page offset
        # in single mode offset is the media ID
        page = int(offset)
    except:
        page = 1

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

    if len(paginated_items) == 1:
        # Render single view
        return render_to_response('gallery/gallery_view.html',
                                  { 'media_item':media_items[0], 'paginator':paginator  },
                                  context_instance=RequestContext(request) )
    else:
        # Render gallery view
        return render_to_response('gallery/gallery_view.html',
                                  { 'media_items':media_items, 'paginator':paginator },
                                  context_instance=RequestContext(request) )

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...

# View all "landscape" items in gallery mode starting on page 3
http://www.example.com/gallery/landscape/multi/3

# View the 45th landscape item in singular mode
http://www.example.com/gallery/landscape/single/45

When the template is rendered, the paginator will offer the has_next and has_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)...

url(r'gallery/(?P<category>.+)/(?P<mode>.+)/(?P<offset>\d+)
, 'whatever.views.media_gallery'),

def media_gallery(request, category, mode, offset):
    """
    Render a media gallery.
    category = media item category filter
    mode = ( multi | single )
    offset = The pagination offset in multi mode or the media ID in single mode
    """

    if mode == 'multi':
        per_page = 20   # or however many items per page

    elif mode == 'single':
        per_page = 1
    else:
        pass    # handle this however

    # Queryitems
    raw_media_items = Media.objects.filter(category=category)

    # Setup paginator
    paginator = Paginator(raw_media_items, per_page)

    try:
        # in multi mode offset is the page offset
        # in single mode offset is the media ID
        page = int(offset)
    except:
        page = 1

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

    if len(paginated_items) == 1:
        # Render single view
        return render_to_response('gallery/gallery_view.html',
                                  { 'media_item':media_items[0], 'paginator':paginator  },
                                  context_instance=RequestContext(request) )
    else:
        # Render gallery view
        return render_to_response('gallery/gallery_view.html',
                                  { 'media_items':media_items, 'paginator':paginator },
                                  context_instance=RequestContext(request) )
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文