如何激活DJANGO.CORE.CONTEXT_PROCESSORS.REQUEST

icn*_*icn 5 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/

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

这是我最初的问题

Django自定义模板标签中的访问请求

在我遵循答案之后

我仍然有错误

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'
Run Code Online (Sandbox Code Playgroud)

导致问题的代码是

request = context ['request'] in

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}
Run Code Online (Sandbox Code Playgroud)

And*_*son 2

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

另请参阅我的答案的评论...您可能也需要这些信息。