How to access the Django request inside a Template Tag
You can access the Django request within a Template Tag by enabling the request context processor.
If you’re using a generic view, for example, ‘direct_to_template’, your render() function will then be passed a RequestContext , and you can access the request by context[‘request’].
If you’re not using a generic view, and are using render_to_response(), then you have to explicitly pass context_instance=RequestContext(request) as the third parameter.
You enable the request context processor by adding this to your settings file:
TEMPLATE_CONTEXT_PROCESSORS = ('django.core.context_processors.request',)
You might also need to add back the default context processors. These vary from version to version - check the django documentation for the settings file for details.
For example, in 0.96, I’ve got this in my settings file
TEMPLATE_CONTEXT_PROCESSORS = ('django.core.context_processors.request',
'django.core.context_processors.auth',
'django.core.context_processors.debug',
'django.core.context_processors.i18n',
)