django自定义模板标记中的访问请求

时间:2010-01-29 06:21:17

标签: python django django-templates django-custom-tags

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?

3 个答案:

答案 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的函数,但它已被弃用。

希望它有所帮助!