小编典典

Django:重新启动后出现“没有名为context_processors的模块”错误

python

我有一个可以在我的PC上运行的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

阅读 132

收藏
2021-01-20

共1个答案

小编典典

问题是升级到Django 1.8后,在settings.py中没有按要求设置模板设置。我还不清楚为什么它可以在使用Django服务器的PC上运行。

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

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',
            ],
        },
    },
]

并将我的旧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',
            ],
        },
    },
]

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

2021-01-20