Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django 1.4: Why is my context processor not working?

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.

like image 919
Matthew Calabresi Avatar asked Aug 19 '12 13:08

Matthew Calabresi


2 Answers

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
            ]
        },
    },
]
like image 159
intelis Avatar answered Oct 27 '22 10:10

intelis


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)

like image 41
supervacuo Avatar answered Oct 27 '22 10:10

supervacuo