Reputation: 487
I recently upgraded a project from Django 1.3 to 1.4, and that seems to have broken my context processor.
In myapp/myapp/processors.py
:
def currentPath(request):
return {'current_path': request.get_full_path()}
In myapp/myapp/settings.py
:
from django.conf import global_settings
global_settings.TEMPLATE_CONTEXT_PROCESSORS += (
'myapp.processors.currentPath',
'django.core.context_processors.request',
)
In any template, {{ current_path }}
is expected -- and did, until the upgrade, return the current path. Now, it is not getting processed at all. I'm absolutely stuck here.
Upvotes: 5
Views: 5300
Reputation: 8058
In Django 1.8
adding processors to TEMPLATE_CONTEXT_PROCESSORS
won't work.
Deprecated since version 1.8:
Set the 'context_processors' option in the OPTIONS of a DjangoTemplates backend instead.
You have to do it like this:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
# insert your TEMPLATE_DIRS here
],
'OPTIONS': {
'context_processors': [
# Insert your TEMPLATE_CONTEXT_PROCESSORS here or use this
# list if you haven't customized them:
'django.contrib.auth.context_processors.auth',
'django.template.context_processors.debug',
'django.template.context_processors.i18n',
'django.template.context_processors.media',
'django.template.context_processors.static',
'django.template.context_processors.tz',
'django.contrib.messages.context_processors.messages',
],
'loaders': [
# insert your TEMPLATE_LOADERS here
]
},
},
]
Upvotes: 6
Reputation: 9242
Just for kicks, would you consider specifying that setting the usual way:
- global_settings.TEMPLATE_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',
+ 'django.core.context_processors.static',
+ 'django.contrib.messages.context_processors.messages',
'myapp.processors.currentPath',
'django.core.context_processors.request',
)
This could eliminate your (useful-looking!) reference to global_settings
as a source of the issue.
Secondly, if you run
manage.py shell
does
from myapp.processors import currentPath
work? Your project structure seems a litte quirky (I haven't seen a context processors in the same directory as settings.py
before; my context.py
is in the same directory as a models.py
, which I understand should usually not be the same directory as settings.py
).
(Converted from comment to answer at OP's request)
Upvotes: 4
Reputation: 32532
You need to include the RequestContext
for the context processors to be processed.
You can use the render
shortcut to have it included automatically:
from django.shortcuts import render
def my_view(request):
context = {}
return render(request, 'mytemplate.html', context)
If you're using classed-based views, the RequestContext
will be included automatically.
Upvotes: 2
Reputation: 7596
You should return dict:
def currentPath(request):
return {'current_path': request.get_full_path()}
Upvotes: 2