Django - 模板上下文处理器 - 打破了我的应用程序 [英] Django - template context processors - breaking my app

查看:111
本文介绍了Django - 模板上下文处理器 - 打破了我的应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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()}

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

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.

推荐答案

Django有一组默认的TEMPLATE_CONTEXT_PROCESSORS,您需要在添加自己的时候手动添加。 http://docs.djangoproject.com/en/1.3/参考/设置/#模板上下文处理器

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

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

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",
)

这篇关于Django - 模板上下文处理器 - 打破了我的应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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