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',
)

4 Responses to “How to access the Django request inside a Template Tag”

  1. Daniel Roseman says:

    Sorry, this is misleading. You are confusing RequestContext with a particular context processor.

    The context processor settings do not determine whether not the context is a RequestContext. This is determined by your view. If you’re using a generic view, then it will always be a RequestContext. But if you’re using render_to_response, you have to explicitly pass context_instance=RequestContext(request) as the third parameter.

    If you do this, your context will be a RequestContext, even if you don’t add the Request context processor to your settings. Of course, if you don’t do that, you won’t have access to the ‘request’ in your template.

  2. Rachel says:

    Thanks for your comments Daniel – I’ve updated my post.

    (My original post was correct in the course of action to take, but poor in its language to explain it)

  3. Nick says:

    Thanks for the post.

    I’ve found that it’s very convinient to use a decorator writing simple line of code like @render_to(‘mytemplate.html’) instead of passing RequestContext every time like context_instance=RequestContext(request) etc

    Here is render_to decorator code. I’ve found it on internet, so I don’t know who is the author.
    How to use it:
    @render_to(‘mytemplate.html’)
    def myview(request):
    return ({‘parameter1: 1, ‘parameter2′:2})

    Code:

    from django.shortcuts import render_to_response
    from django.template import RequestContext

    def render_to(template):
    “”"
    Decorator for Django views that sends returned dict to render_to_response function
    with given template and RequestContext as context instance.

    If view doesn’t return dict then decorator simply returns output.
    Additionally view can return two-tuple, which must contain dict as first
    element and string with template name as second. This string will
    override template name, given as parameter

    Parameters:

    – template: template name to use
    “”"
    def renderer(func):
    def wrapper(request, *args, **kw):
    output = func(request, *args, **kw)
    if isinstance(output, (list, tuple)):
    return render_to_response(output[1], output[0], RequestContext(request))
    elif isinstance(output, dict):
    return render_to_response(template, output, RequestContext(request))
    return output
    return wrapper
    return renderer

  4. Zack says:

    Thanks for the post, many people talk about using context['request'], but not many tell you about needing
    TEMPLATE_CONTEXT_PROCESSORS = (‘django.core.context_processors.request’,)
    to get it to work.

    I was using the context to directly get the GET request like: context['urlinfo'], but it didn’t work after sending to a flatpage template, but thanks to you its works like a charm, and I can do this context['request'].GET['urlinfo'].

    Thanks!

Leave a Reply