Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access request in django custom template tags

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 In Django, is it possible to access the current user session from within a custom tag?.

like image 824
icn Avatar asked Jan 29 '10 06:01

icn


People also ask

How do I use custom template tags in Django?

Create a custom template tagUnder the application directory, create the templatetags package (it should contain the __init__.py file). For example, Django/DjangoApp/templatetags. In the templatetags package, create a . py file, for example my_custom_tags, and add some code to it to declare a custom tag.

How many arguments does the tag method uses for registering custom tags?

Registering the tag The tag() method takes two arguments: The name of the template tag – a string. If this is left out, the name of the compilation function will be used. The compilation function – a Python function (not the name of the function as a string).

What are Django template tags?

Django Code The template tags are a way of telling Django that here comes something else than plain HTML. The template tags allows us to to do some programming on the server before sending HTML to the client.


2 Answers

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} 
like image 143
Ignacio Vazquez-Abrams Avatar answered Sep 18 '22 16:09

Ignacio Vazquez-Abrams


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

So in main view method you should add the following line:

from django.template import RequestContext         return render_to_response('index.html', {'form': form, },                                context_instance = RequestContext(request)) 
like image 26
Andriy Kopachevskyy Avatar answered Sep 20 '22 16:09

Andriy Kopachevskyy