Django:重启后"没有名为context_processors的模块"错误

Chr*_*May 26 python django django-middleware django-settings

我有一个可以在我的电脑上运行的Django站点,并在加载后在我的服务器上工作.我注意到我的服务器有Django 1.6,我升级到1.8.

重新启动后,我的网站上没有任何页面加载,我收到错误:

ImportError没有名为context_processors的模块

我阅读了关于Django和allauth的文档.Django中提到,在1.8 context_processors移动和allauth说具体allauth标签不再需要在TEMPLATE_CONTEXT_PROCESSORSsettings.py.

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

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

其他人遇到这个?我是在正确的轨道上吗?我需要在设置中更改某些内容吗?我无法确定它是否是Django或allauth问题所以不确定从哪里开始.

任何帮助表示赞赏!

追溯:

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
Run Code Online (Sandbox Code Playgroud)

小智 55

我遇到了同样的问题,但我从1.9.1升级到1.10.我发现设置有一点不同.

这是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',
        ],
    },
},
]
Run Code Online (Sandbox Code Playgroud)

这是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',
        ],
    },
},
]
Run Code Online (Sandbox Code Playgroud)

该行在django.core.context_processors.request1.10中无效.删除它,代码运行良好.

  • 我认为你应该将`django.core.context_processors`改为`django.template.context_processors`.请参阅[docs](https://docs.djangoproject.com/en/1.10/releases/1.8/#django-core-context-processors).只需删除标记建议的行,就会在没有上下文请求的情况下离开.无论如何,谢谢你让我进入了正确的方向! (30认同)

Chr*_*May 13

问题是我在升级到Django 1.8之后根据需要在settings.py中没有TEMPLATES设置.我不清楚为什么它使用Django服务器在我的电脑上工作.

从allauth docs中,我将其粘贴到我的设置文件中:

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',
            ],
        },
    },
]
Run Code Online (Sandbox Code Playgroud)

并将旧TEMPLATE_DIRS设置的内容复制到TEMPLATES的DIRS定义中.最终结果如下:

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',
            ],
        },
    },
]
Run Code Online (Sandbox Code Playgroud)

根据最近的allauth更新文档,context_processors现在需要在TEMPLATES设置中指定而不是TEMPLATE_CONTEXT_PROCESSORS设置.

感谢Joey Wilhelm指出我正确的方向.

  • 啊好.我想知道是否会出现这种情况.Django 1.8引入了多个数据库后端,因此它使用了新的设置.您可能需要查看发行说明,以确保不需要更新其他主要更改.另外,您可能想要运行两个好的命令:`manage.py check`和`manage.py diffsettings`. (2认同)