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?
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
rendershortcut instead ofrender_to_response, which creates a RequestContext for you.