我在设置 django-pagination 时遇到问题

发布于 2024-12-09 03:26:28 字数 1789 浏览 1 评论 0原文

我正在为 Django 网站制作一个模板(它是报价数据库)。我想要像 Digg 一样的分页。尽管如此,该应用程序的作者已经制作了自己的分页,不幸的是没有页面编号(只是“上一个”和“下一个”链接)。所以我已经安装了 django-pagination,但我无法在该网站上使用它。我对 Django 完全陌生,甚至编程 - 我只是一个简单的网页设计师......好吧,我们开始吧。

有原始脚本: https://bitbucket.org/fleg/fqdb/

第一件事是模板上下文处理器的问题。我的 settings.py 没有这个部分,所以我添加它就像在 django-pagination 文档中一样。当我运行该网站时,出现错误:“将 'django.contrib.auth.context_processors.auth' 放入 TEMPLATE_CONTEXT_PROCESSORS 设置中以便使用管理应用程序”。那么我该如何订购呢?

第二个问题是模板。我使用它完全就像截屏视频

{% extends "fqdb/base.html" %}
{% load pagination_tags %}

{% block title %}{{ title }}{% endblock %}

{% block content %}
            <h1>{{ title }}</h1>

            {% if quotes %}
            {% autopaginate quotes %}
            {% for quote in quotes %}
                {% include 'fqdb/quote_body.html' %}
            {% endfor %}
            {% paginate %}
            {% else %}
                <p>Brak cytatów.</p>
            {% endif %}
{% endblock %}

但我收到“模板错误:渲染时捕获 KeyError:请求”。但是...说真的,我不知道这段代码有什么问题!

有分页视图 - 报价列表。它无需分页即可工作,所以我认为这不是问题,但也许是。

def list_paged(request, page, order_by_what, title, reverse_name):
hash = get_ip_hash(request)
lista = Quote.objects.filter(accepted = True).order_by(order_by_what)[:]
    returnDict = {'quotes': lista, 'title': title, 'hash': hash, 'sidebar': get_sidebar()}
    return render_to_response('fqdb/quote_list.html', {'quotes': get_quotes(quotes)},     context_instance=RequestContext(request))

我已将其修改为不分页,因为它是 django 分页任务。您可以在 Bitbucket 上找到原始视图。

也许您知道一些更好的分页解决方案?

I'm making a template for Django site (it's quote database). I wanna have Digg-like pagination. Altough, author of the application has made his own pagination, unfortunately without page numering (just "previous" and "next" links). So I've installed django-pagination, but I can't use it with the site. I'm completly new in Django, even programming - I'm just a simple webdesigner... OK, here we go.

There is the original script: https://bitbucket.org/fleg/fqdb/

The first thing is a problem with template context processors. My settings.py didn't have this section, so I added it exactly like in django-pagination documentation. When I run the site, I get an error: "Put 'django.contrib.auth.context_processors.auth' in your TEMPLATE_CONTEXT_PROCESSORS setting in order to use the admin application". So how I have to order that?

A second problem is template. I use it exactly like on the screencast:

{% extends "fqdb/base.html" %}
{% load pagination_tags %}

{% block title %}{{ title }}{% endblock %}

{% block content %}
            <h1>{{ title }}</h1>

            {% if quotes %}
            {% autopaginate quotes %}
            {% for quote in quotes %}
                {% include 'fqdb/quote_body.html' %}
            {% endfor %}
            {% paginate %}
            {% else %}
                <p>Brak cytatów.</p>
            {% endif %}
{% endblock %}

But I get "Template error: Caught KeyError while rendering: request". But... Seriously, I don't know what's wrong with this code!

There is the paginated view - quote list. It work without pagination, so I don't think if it's a problem, but maybe.

def list_paged(request, page, order_by_what, title, reverse_name):
hash = get_ip_hash(request)
lista = Quote.objects.filter(accepted = True).order_by(order_by_what)[:]
    returnDict = {'quotes': lista, 'title': title, 'hash': hash, 'sidebar': get_sidebar()}
    return render_to_response('fqdb/quote_list.html', {'quotes': get_quotes(quotes)},     context_instance=RequestContext(request))

I have modified it to not paginating, because it's django-pagination task. You can find original view on Bitbucket.

Maybe do you know some better pagination solutions?

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

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

发布评论

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

评论(3

同展鸳鸯锦 2024-12-16 03:26:28

看起来您需要添加 django.contrib.auth.context_processors.authdjango.core.context_processors.request 上下文处理器到您的TEMPLATE_CONTEXT_PROCESSORS 设置。

在定义 TEMPLATE_CONTEXT_PROCESSORS 之前,django 将使用默认值。看起来您的某些代码似乎需要 auth 处理器,因此您出现了第一条错误消息。

在我看来,KeyError 就好像您需要request 处理器一样。

在您的设置文件中尝试以下操作:

TEMPLATE_CONTEXT_PROCESSORS = (
    "django.contrib.auth.context_processors.auth",
    #"django.core.context_processors.debug",
    "django.core.context_processors.i18n",
    "django.core.context_processors.media",
    #"django.core.context_processors.static",
    #"django.contrib.messages.context_processors.messages")
    "django.core.context_processors.request"
    )

我使用了 Django 1.3 请求上下文文档,添加了请求处理器,并注释掉了您似乎不需要的那些。

模板上下文处理器的顺序通常并不重要,只要它们不定义重叠的变量名称即可。

It looks like you need to add django.contrib.auth.context_processors.auth and django.core.context_processors.request context processors to your TEMPLATE_CONTEXT_PROCESSORS setting.

Before you defined TEMPLATE_CONTEXT_PROCESSORS, django would have used the default. It looks as if some of your code requires the auth processor, hence your first error message.

The KeyError looks to me as if you require the request processor.

Try the following in your settings file:

TEMPLATE_CONTEXT_PROCESSORS = (
    "django.contrib.auth.context_processors.auth",
    #"django.core.context_processors.debug",
    "django.core.context_processors.i18n",
    "django.core.context_processors.media",
    #"django.core.context_processors.static",
    #"django.contrib.messages.context_processors.messages")
    "django.core.context_processors.request"
    )

I've used the default list given in the Django 1.3 request context docs, added the request processor, and commented out the ones that you don't seem to need.

The order of template context processors does not usually matter, as long as they do not define overlapping variable names.

一身仙ぐ女味 2024-12-16 03:26:28

如果对象是从模板标签传递的,

def comment_app(context):
    objects = Comments.objects.get_tree_for_object(context['content_object'])
    return {
        'comments_tree': objects,
        'request': context['request']
}
register.inclusion_tag('comments/comment_app.html', takes_context=True)(comment_app)

请注意:'request': context['request']

If the objects are passed from a templatetag

def comment_app(context):
    objects = Comments.objects.get_tree_for_object(context['content_object'])
    return {
        'comments_tree': objects,
        'request': context['request']
}
register.inclusion_tag('comments/comment_app.html', takes_context=True)(comment_app)

note the: 'request': context['request']

尛丟丟 2024-12-16 03:26:28
{% autopaginate quotes N%}

N - 每页需要多少个项目

{% autopaginate quotes N%}

N - how many items you need for each page

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