Django: “No module named context_processors” error

2019-01-24 00:17发布

问题:

I have a Django site that works on my PC, and was working briefly on my server after loading it on. I noticed my server had Django 1.6 and I upgraded to 1.8.

After rebooting, none of the pages on my site load and I get the error:

ImportError No module named context_processors

I read through the docs on Django and allauth. Django mentions that in 1.8 the context_processors moved and allauth says specific allauth tags are no longer needed in the TEMPLATE_CONTEXT_PROCESSORS of settings.py.

Django: https://docs.djangoproject.com/en/1.8/ref/settings/

Allauth: https://django-allauth.readthedocs.org/en/latest/installation.html

Anyone else run into this? Am I on the right track? Do I need to change something in settings? I can't really tell if it's a Django or allauth issue so not sure where to start.

Any help is appreciated!

Traceback:

Django Version: 1.8.4
Python Version: 2.7.6
Installed Applications:
('django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'plant',
 'journal',
 'userimg',
 'django.contrib.sites',
 'allauth',
 'allauth.account')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware')


Traceback:
File "/usr/local/lib/python2.7/dist-packages/Django-1.8.4-py2.7.egg/django/core/handlers/base.py" in get_response
  132.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/django/django_project/plant/views.py" in plant_main
  24.     return render(request, 'plant/plant_main.html', context)
File "/usr/local/lib/python2.7/dist-packages/Django-1.8.4-py2.7.egg/django/shortcuts.py" in render
  67.             template_name, context, request=request, using=using)
File "/usr/local/lib/python2.7/dist-packages/Django-1.8.4-py2.7.egg/django/template/loader.py" in render_to_string
  99.         return template.render(context, request)
File "/usr/local/lib/python2.7/dist-packages/Django-1.8.4-py2.7.egg/django/template/backends/django.py" in render
  74.         return self.template.render(context)
File "/usr/local/lib/python2.7/dist-packages/Django-1.8.4-py2.7.egg/django/template/base.py" in render
  208.                 with context.bind_template(self):
File "/usr/lib/python2.7/contextlib.py" in __enter__
  17.             return self.gen.next()
File "/usr/local/lib/python2.7/dist-packages/Django-1.8.4-py2.7.egg/django/template/context.py" in bind_template
  237.         processors = (template.engine.template_context_processors +
File "/usr/local/lib/python2.7/dist-packages/Django-1.8.4-py2.7.egg/django/utils/functional.py" in __get__
  60.         res = instance.__dict__[self.name] = self.func(instance)
File "/usr/local/lib/python2.7/dist-packages/Django-1.8.4-py2.7.egg/django/template/engine.py" in template_context_processors
  90.         return tuple(import_string(path) for path in context_processors)
File "/usr/local/lib/python2.7/dist-packages/Django-1.8.4-py2.7.egg/django/template/engine.py" in <genexpr>
  90.         return tuple(import_string(path) for path in context_processors)
File "/usr/local/lib/python2.7/dist-packages/Django-1.8.4-py2.7.egg/django/utils/module_loading.py" in import_string
  26.     module = import_module(module_path)
File "/usr/lib/python2.7/importlib/__init__.py" in import_module
  37.     __import__(name)

Exception Type: ImportError at /plant/
Exception Value: No module named context_processors

回答1:

I encountered the same problem but I am upgrading from 1.9.1 to 1.10. I found there's a little difference in the settings.

This is the code from 1.9.1

TEMPLATES = [
{
    'BACKEND': 'django.template.backends.django.DjangoTemplates',
    'DIRS': [os.path.join(BASE_DIR, 'templates')],
    'APP_DIRS': True,
    'OPTIONS': {
        'context_processors': [
            'django.template.context_processors.debug',
            'django.template.context_processors.request',
            'django.core.context_processors.request',
            'django.contrib.auth.context_processors.auth',
            'django.contrib.messages.context_processors.messages',
        ],
    },
},
]

This is code for 1.10

TEMPLATES = [
{
    'BACKEND': 'django.template.backends.django.DjangoTemplates',
    'DIRS': [os.path.join(BASE_DIR, 'templates')],
    'APP_DIRS': True,
    'OPTIONS': {
        'context_processors': [
            'django.template.context_processors.debug',
            'django.template.context_processors.request',
            'django.contrib.auth.context_processors.auth',
            'django.contrib.messages.context_processors.messages',
        ],
    },
},
]

The line django.core.context_processors.request is not valid in 1.10. Remove it and the code works well.



回答2:

The issue was that I had no TEMPLATES setting in settings.py as required after upgrading to Django 1.8. I'm not really clear why it was working on my PC using the Django server.

From the allauth docs, I pasted this into my settings file:

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                # Already defined Django-related contexts here

                # `allauth` needs this from django
                'django.template.context_processors.request',
            ],
        },
    },
]

And copied the contents of my old TEMPLATE_DIRS setting into the DIRS definition for TEMPLATES. The final result looks like this:

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'templates')],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                # Already defined Django-related contexts here

                # `allauth` needs this from django
                'django.template.context_processors.request',
            ],
        },
    },
]

According to the documentation for a recent allauth update, context_processors now need to be specified in the TEMPLATES setting and not TEMPLATE_CONTEXT_PROCESSORS setting.

Thanks to Joey Wilhelm for pointing me in the right direction on this.



回答3:

Just a tip: When a traceback doesn't provide you with the information you need to identify the exact line of code; It can be helpful to enable DEBUG mode, and open the page in the browser. There's this wonderful little local_vars element, where you can see local variable state when the traceback occurs. It can be super handy!

(In my case, it was related to changes within allauth)



回答4:

In my case, I had to delete the following line in settings.py:

            'django.core.context_processors.csrf',

I rebooted the server and I didn't see that error again afterwards.