我读了这个
DJANGO.CORE.CONTEXT_PROCESSORS.REQUEST
If
TEMPLATE_CONTEXT_PROCESSORS
contains this processor, every RequestContext will contain a variable request, which is the current HttpRequest. Note that this processor is not enabled by default; you'll have to activate it.
来自 this page
但似乎没有信息如何激活这个处理器。
这是我原来的问题
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'
导致问题的代码是
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}
最佳答案
我在这里回答:How can I pass data to any template from any view in Django?
另请参阅对我的回答的评论……您可能也需要这些信息。
关于django - 如何激活 DJANGO.CORE.CONTEXT_PROCESSORS.REQUEST,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2202328/