Django: “No module named context_processors” error after reboot

南笙酒味 提交于 2019-11-29 02:55:08
mark

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.

Chris May

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.

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)

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.

Now it is updated source: https://docs.djangoproject.com/en/1.11/ref/templates/upgrading/

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [
            # insert your TEMPLATE_DIRS here
        ],
        'APP_DIRS': True,
        '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',
            ],
        },
    },
]
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!