myapp_extras.py中的代码:
from django import template
register = template.Library()
@register.inclusion_tag('new/userinfo.html')
def address():
address = request.session['address']
return {'address':address}
TEMPLATE_CONTEXT_PROCESSORS =(
"django.core.context_processors.auth",
"django.core.context_processors.debug",
"django.core.context_processors.i18n",
"django.core.context_processors.media",
'django.core.context_processors.request'
)
但是我收到了一个错误:
TemplateSyntaxError at /items/
Caught an exception while rendering: global name 'request' is not defined
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 915, in render
dict = func(*args)
File "C:\p4\projects\myproject\..\myproject\invoice\templatetags\myapp_extras.py", line 9, in address
address = request.session['address']
NameError: global name 'request' is not defined
我引用了这个 In Django, is it possible to access the current user session from within a custom tag?
答案 0 :(得分:146)
request
不是该范围内的变量。您必须先从上下文中获取它。 Pass takes_context
to the decorator and add context
to the tag arguments
像这样:
@register.inclusion_tag('new/userinfo.html', takes_context=True)
def address(context):
request = context['request']
address = request.session['address']
return {'address':address}
答案 1 :(得分:11)
我已经尝试过上面的解决方案(来自Ignacio Vazquez-Abrams),直到我发现上下文处理器只适用于RequestContext
包装器类,它才真正起作用。
因此在主视图方法中,您应该添加以下行:
from django.template import RequestContext
return render_to_response('index.html', {'form': form, },
context_instance = RequestContext(request))
答案 2 :(得分:7)
我这样做了:
from django import template
register = template.Library()
def do_test_request(parser,token):
try:
tag_name = token.split_contents() # Not really useful
except ValueError:
raise template.TemplateSyntaxError("%r error" % token.contents.split()[0])
return RequestTestNode()
class RequestTestNode(template.Node):
def __init__(self,):
self.request = template.Variable('request')
def render(self, context):
rqst = self.request.resolve(context)
return "The URL is: %s" % rqst.get_full_path()
register.tag('test_request', do_test_request)
还有一个名为resolve_variable
的函数,但它已被弃用。
希望它有所帮助!