views:

1517

answers:

2

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:\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

I referenced this one http://stackoverflow.com/questions/335231/in-django-is-it-possible-to-access-the-current-user-session-from-within-a-custom.

+11  A: 

request is not a variable in that scope. You will have to get it from the context first. Pass takes_context to the decorator and add context to the tag arguments.

Like this:

@register.inclusion_tag('new/userinfo.html', takes_context = True)
def address(context):
    request = context['request']
    address = request.session['address']
    return {'address':address}
Ignacio Vazquez-Abrams
@Ignacio - great answer - hope you don't mind me editing in an example (and congrats on 10K btw).
Dominic Rodger
@Dominic: The example was a bit off. And thanks.
Ignacio Vazquez-Abrams
@Ignacio - d'oh - thanks for fixing it!
Dominic Rodger
it seems i still got errorsTemplateSyntaxError 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'
xlione
i noticed in this page http://docs.djangoproject.com/en/dev/ref/templates/api/"DJANGO.CORE.CONTEXT_PROCESSORS.REQUESTIf TEMPLATE_CONTEXT_PROCESSORS contains this processor, every RequestContext will contain a variable request, which is the current HttpRequest. Note that this processor is not enabled by default; you'll have to activate it."somewhere i need to configure to activate request processor
xlione
+1  A: 

I've tried solution from above (from Ignacio Vazquez-Abrams) and it actually wasn't works until I've found out that context processors works only with RequestContext wrapper class.

So in main view method should be next line:

from django.template import RequestContext        
return render_to_response('index.html', {'form': form, }, 
                              context_instance = RequestContext(request))
abovesun