My code in 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}
in 'settings.py':
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'
)
but I got an error:
TemplateSyntaxError at /items/
Caught an exception while rendering: global name 'request' is not defined
Original Traceback (most recent call last):
File "C:Python25libsite-packagesdjangoemplatedebug.py", line 71, in render_node
result = node.render(context)
File "C:Python25libsite-packagesdjangoemplate\__init__.py", line 915, in render
dict = func(*args)
File "C:p4projectsmyproject..myprojectinvoiceemplatetagsmyapp_extras.py", line 9, in address
address = request.session['address']
NameError: global name 'request' is not defined
I referenced this one
In Django, is it possible to access the current user session from within a custom tag?.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…