Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is this Django template context processor not applied for all requests?

I've run in to the following Django template context processor problem.

The context processor is defined in myapp/context_processors.py:

def my_context_processor(request):
    return {
        'foo': 123,
    }

It is wired up in settings.py along with the standard Django context processors:

TEMPLATE_CONTEXT_PROCESSORS = (
    'django.contrib.auth.context_processors.auth',
    'django.core.context_processors.debug',
    'django.core.context_processors.i18n',
    'django.core.context_processors.media',
    'myproject.myapp.context_processors.my_context_processor',
)

The problem I'm encountering is that my_context_processor is not applied for all requests.

It is not applied for the following code:

def index(request):
    return render_to_response("index.html", locals())

However, it is applied for the following code:

def index(request):
    return render_to_response("index.html", locals(), context_instance=RequestContext(request))

I was under the impression that context processors are applied for ALL requests, not just when context_instance is provided.

How do I make my context processors being applied for ALL requests?

like image 710
knorv Avatar asked Jul 11 '11 16:07

knorv


People also ask

What is template context processor in Django?

context_processors is a list of dotted Python paths to callables that are used to populate the context when a template is rendered with a request. These callables take a request object as their argument and return a dict of items to be merged into the context.

What does {% %} mean in Django?

{% %} and {{ }} are part of Django templating language. They are used to pass the variables from views to template. {% %} is basically used when you have an expression and are called tags while {{ }} is used to simply access the variable.

What is request context in Django?

RequestContext adds a bunch of variables to your template context by default – things like the HttpRequest object or information about the currently logged-in user. The render() shortcut creates a RequestContext unless it's passed a different context instance explicitly.


2 Answers

You have answered your own question. It's applied to the responses that use RequestContext. It's not applied to the ones that don't.

The way to get it to be applied to all responses is to make sure you always use RequestContext. Or, in Django 1.3+, you can use the new render shortcut instead of render_to_response, which creates a RequestContext for you.

like image 136
Daniel Roseman Avatar answered Sep 25 '22 02:09

Daniel Roseman


Django introduced a new render shortcut in Django 1.3 which automatically includes the RequestContext

from django.shortcuts import render

def my_view(request):
    # View code here...
    context = {
        'some_extra_var_for_template': 'value'
    }
    return render(request, 'myapp/index.html', context)

You can read about it in the Django docs.

like image 36
Craig Blaszczyk Avatar answered Sep 25 '22 02:09

Craig Blaszczyk