我的代碼:訪問請求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}
在 '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'
)
,但我得到了一個錯誤:
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?。
嗯......該鏈接問題的答案似乎是錯誤的... – 2010-01-29 06:35:42
如果你想訪問請求檢查這個答案http://stackoverflow.com/a/2567234/1153703 – 2016-07-12 05:02:11