【发布时间】:2011-01-13 05:24:41
【问题描述】:
我读过这个
DJANGO.CORE.CONTEXT_PROCESSORS.REQUEST如果
TEMPLATE_CONTEXT_PROCESSORS包含此处理器,则每个 RequestContext 将包含一个变量请求,即当前 Http请求。请注意,默认情况下不启用此处理器; 你必须激活它。
但似乎没有关于如何激活此处理器的信息。
这是我最初的问题
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'] 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}
【问题讨论】:
-
看起来在 Google 网上帮助您的人很有帮助。您应该发布他要求的代码。
标签: django django-templates django-custom-tags