Python Django:重启后“没有名为context_processors的模块”错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32828536/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 12:20:40  来源:igfitidea点击:

Django: "No module named context_processors" error after reboot

pythondjangodjango-settingsdjango-middleware

提问by Chris May

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.

我有一个可在我的 PC 上运行的 Django 站点,并且在加载它后在我的服务器上短暂运行。我注意到我的服务器有 Django 1.6,我升级到 1.8。

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

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

ImportError No module named context_processors

ImportError 没有名为 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_PROCESSORSof settings.py.

我通读了关于 Django 和 allauth 的文档。Django 提到在 1.8 中 context_processors 移动了,allauth 说在TEMPLATE_CONTEXT_PROCESSORSof 中不再需要特定的 allauth 标签settings.py

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

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

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

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.

还有人遇到这个吗?我在正确的轨道上吗?我需要在设置中更改某些内容吗?我真的不知道这是 Django 还是 allauth 问题,所以不确定从哪里开始。

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.clickHymaning.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

采纳答案by 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.

问题是在升级到 Django 1.8 后,我在 settings.py 中没有按要求设置 TEMPLATES。我不太清楚为什么它使用 Django 服务器在我的 PC 上工作。

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

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

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

并将我的旧TEMPLATE_DIRS设置的内容复制到模板的 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',
            ],
        },
    },
]

According to the documentation for a recent allauth update, context_processorsnow need to be specified in the TEMPLATES setting and not TEMPLATE_CONTEXT_PROCESSORSsetting.

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

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

感谢Joey Wilhelm为我指明了正确的方向。

回答by 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.

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

This is the code from 1.9.1

这是来自 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

这是 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.requestis not valid in 1.10. Remove it and the code works well.

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

回答by Aaron

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 DEBUGmode, and open the page in the browser. There's this wonderful little local_varselement, where you can see local variable state when the traceback occurs. It can be super handy!

只是一个提示:当回溯没有为您提供识别确切代码行所需的信息时;启用DEBUG模式并在浏览器中打开页面会很有帮助。有这个奇妙的小local_vars元素,您可以在其中看到回溯发生时的局部变量状态。它可以非常方便!

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

(就我而言,这与 allauth 中的更改有关)

回答by ndarriulat

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

就我而言,我必须删除 settings.py 中的以下行:

            'django.core.context_processors.csrf',

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

我重新启动了服务器,之后我没有再次看到该错误。

回答by Pradeep Kumar Nalluri

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

现在更新源: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',
            ],
        },
    },
]