如何激活DJANGO.CORE.CONTEXT_PROCESSORS.REQUEST

时间:2010-02-04 18:55:14

标签: django django-templates django-custom-tags

我读了这个

“DJANGO.CORE.CONTEXT_PROCESSORS.REQUEST 如果TEMPLATE_CONTEXT_PROCESSORS包含此处理器,则每个 RequestContext将包含一个变量请求,它是当前的 HttpRequest的。请注意,默认情况下不启用此处理器; 你必须激活它。 “从这个页面

http://docs.djangoproject.com/en/dev/ref/templates/api/

但似乎没有关于如何激活此处理器的信息。

这是我原来的问题

Access request in django custom template tags

我跟着答案后

我仍然有错误

TemplateSyntaxError at / Caught an exception while rendering: 'request' Original Traceback (most recent call last): 
File "C:\Python25\lib\site-packages\django\template\debug.py", line 71, in render_node result = node.render(context) 
File "C:\Python25\lib\site-packages\django\template__init__.py", line 936, in render dict = func(*args)
 File "c:\...\myapp_extras.py", line 7, in login request = context['request'] 
File "C:\Python25\lib\site-packages\django\template\context.py", line 44, in getitem raise KeyError(key) KeyError: 'request'

造成问题的代码是

中请求= context ['request']
from django import template

register = template.Library()


@register.inclusion_tag('userinfo.html',takes_context = True)
def userinfo(context):
 request = context['request']
 address = request.session['address']
 return {'address':address}

2 个答案:

答案 0 :(得分:2)

我在这里回答:How can I pass data to any template from any view in Django?

另见我的回答评论......你可能也想要那些信息。

答案 1 :(得分:2)

在settings.py

from django.conf import global_settings

TEMPLATE_CONTEXT_PROCESSORS = global_settings.TEMPLATE_CONTEXT_PROCESSORS + (
    'django.core.context_processors.request',
)