Django分页和“当前页” [英] Django pagination and "current page"

查看:139
本文介绍了Django分页和“当前页”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个Django应用程序,它将利用臭名昭着的分页技术。我试图找出django.core.paginator模块的工作原理。

I'm currently developing a Django application which will make use of the infamous "pagination" technique. I'm trying to figure out how the django.core.paginator module works.

我有一个包含问题模型的应用程序。我将使用此分页列出所有问题。每页将有20个问题。

I have an application with a Question model. I will be listing all of the questions using this paginator. There will be 20 questions per page.

def show_question(question_pk):
    questions = Question.objects.all()
    paginator = Paginator(questions, 20)
    page      = ... # Somehow figure out which page the question is on
    return render_to_response('show_question.html', { 'page' : page })

在视图中,我将不同的页面列为... 2,3 , 4 ,5,6,...我想像许多页面一样突出显示当前页面。

In the view, where I list the different pages as "... 2, 3, 4, 5, 6, ..." I want to highlight the current page somehow, like many pages do.

真的有我想知道两件事:

There are really two things I want to know:


  1. 如何让Django弄清楚问题所在的页面?

  2. 如何正确地写我的模板突出显示当前访问的页面?

编辑:对不起,我忘了这个问题的一部分。我也希望任何页面,除了当前的页面,链接到 / questions / {{that_page.start_index}} 。所以基本上每个页面的链接都会链接到该页面的第一个问题。

Sorry, I forgot part of this question. I would also like any page except for the current one to be a link to /questions/{{ that_page.start_index }}. So basically every page link would link to the first question on that page.

推荐答案

你不想做ol'GET参数,这是django.core.paginator是为了使用而编写的。为了做你想要的,我可以想到没有比预先计算每个问题的页面更好的方法。例如,您的观点将最终如下:

Hmm... I see from your comment that you don't want to do the ol' GET parameter, which is what django.core.paginator was written for using. To do what you want, I can think of no better way than to precompute the page that each question is on. As an example, your view will end up being something like:

ITEMS_PER_PAGE = 20
def show_question(question_pk):
    questions = Question.objects.all()
    for index, question in enumerate(questions):
        question.page = ((index - 1) / ITEMS_PER_PAGE) + 1
    paginator = Paginator(questions, ITEMS_PER_PAGE)
    page = paginator.page(questions.get(pk=question_pk).page)
    return render_to_response('show_question.html', { 'page' : page })

要突出显示模板中的当前页面,您可以执行类似于

To highlight the current page in the template, you'd do something like

{% for i in page.paginator.page_range %}
    {% ifequal i page.number %}
        <!-- Do something special for this page -->
    {% else %}
        <!-- All the other pages -->
    {% endifequal %}
{% endfor %}

至于项目,您将有两个不同的object_lists来处理...

As for the items, you'll have two different object_lists to work with...

page.object_list

将是当前页面中的对象,

will be the objects in the current page and

page.paginator.object_list

将是所有对象,无论页面如何。这些项目中的每一个都将有一个页面变量,它将告诉您他们正在使用哪个页面。

will be all objects, regardless of page. Each of those items will have a "page" variable that will tell you which page they're on.

所有这些都说,你在做什么听起来是非常规的。你可能想重新思考,但无论如何,祝你好运。

That all said, what you're doing sounds unconventional. You may want to rethink, but either way, good luck.

这篇关于Django分页和“当前页”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆