Django - 模板上下文处理器 - 破坏我的应用程序

发布于 2024-11-09 09:02:30 字数 821 浏览 0 评论 0原文

我试图设置一个模板上下文处理器,如 这篇文章提及,以便我可以为每个模板提供信息。

我在views.py中编写了这个函数:

def items_in_cart(request):
    """Used by settings.TEMPLATE_CONTEXT_PROCESSORS to provide an item count
    to every template"""
    cart, lines = get_users_cart_and_lines(request)
    return {'items_in_cart': lines.count()}

然后我将这一行添加到settings.py:

TEMPLATE_CONTEXT_PROCESSORS = ('Store.views.items_in_cart',)

但是现在每当我进入任何页面时我都会收到此错误:

ImproperlyConfigured at /

Put 'django.contrib.auth.context_processors.auth' in your TEMPLATE_CONTEXT_PROCESSORS setting in order to use the admin application.

我做错了什么吗?这是怎么回事?我尝试按照错误所述执行操作,然后它将呈现一个页面,其中缺少所有样式表和图像。

I was trying to set up a template context processor like this article mentions so that I could provide information to every template.

I wrote this function in views.py:

def items_in_cart(request):
    """Used by settings.TEMPLATE_CONTEXT_PROCESSORS to provide an item count
    to every template"""
    cart, lines = get_users_cart_and_lines(request)
    return {'items_in_cart': lines.count()}

And then I added this line to settings.py:

TEMPLATE_CONTEXT_PROCESSORS = ('Store.views.items_in_cart',)

But now whenever I go to any page I get this error:

ImproperlyConfigured at /

Put 'django.contrib.auth.context_processors.auth' in your TEMPLATE_CONTEXT_PROCESSORS setting in order to use the admin application.

Did I do something wrong? What's going on here? I tried doing what the error said, and then it will render a page with all of my style sheets and images missing.

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

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

发布评论

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

评论(1

情定在深秋 2024-11-16 09:02:30

Django 有一组默认的 TEMPLATE_CONTEXT_PROCESSORS,您需要在添加自己的时手动添加。 http://docs.djangoproject.com/en/1.3/ ref/settings/#template-context-processors

根据您的 Django 版本,这些内容有所不同,但是如果使用 Django 1.3,您可能会看到如下内容

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",
    "Store.views.items_in_cart",
)

Django has a default set of TEMPLATE_CONTEXT_PROCESSORS, which you need to manually add when adding your own. http://docs.djangoproject.com/en/1.3/ref/settings/#template-context-processors

Depending on your Django version these are different, however if using Django 1.3 you might have something as follows

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