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

Posted

技术标签:

【中文标题】Django:重新启动后出现“没有名为 context_processors 的模块”错误【英文标题】:Django: "No module named context_processors" error after reboot 【发布时间】:2015-12-26 00:32:21 【问题描述】:

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

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

ImportError 没有名为 context_processors 的模块

我阅读了有关 Django 和 allauth 的文档。 Django 提到在 1.8 中移动了 context_processors 并且 allauth 说在 TEMPLATE_CONTEXT_PROCESSORSsettings.py 中不再需要特定的 allauth 标签。

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

阿劳斯: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

【问题讨论】:

你能发布你的错误的完整回溯吗?这将是一个更有帮助的起点。 刚刚更新。打算第一次做。对此感到抱歉。 您是否在模板设置中为context_processors 设置了任何内容?从外观上看,您可能在其中列出了一个实际上不包含任何上下文处理器的应用程序。 【参考方案1】:

问题是我升级到 Django 1.8 后在 settings.py 中没有 TEMPLATES 设置。我不太清楚为什么它使用 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 更新文档,现在需要在 TEMPLATES 设置中指定 context_processors 而不是 TEMPLATE_CONTEXT_PROCESSORS 设置。

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

【讨论】:

啊好。我想知道是否可能是这种情况。 Django 1.8 引入了多个数据库后端,因此它使用了新设置。您可能需要查看发行说明,以确保没有其他需要更新的重大更改。此外,您可能需要运行两个很好的命令:manage.py checkmanage.py diffsettings 我不太清楚为什么它可以在我的电脑上运行...也许升级后你没有删除 *.pyc 文件?当然,这是一个我不期待答案的问题。这只是为了考虑它;)【参考方案2】:

我遇到了同样的问题,但我正在从 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',
        ],
    ,
,
]

这是 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',
        ],
    ,
,
]

django.core.context_processors.request 行在 1.10 中无效。删除它,代码运行良好。

【讨论】:

我认为您应该将django.core.context_processors 更改为django.template.context_processors。请参阅docs。只需按照标记建议删除该行,就会使您在上下文中没有请求。无论如何,谢谢,因为你让我进入了正确的方向! @Jeewes 是的,核心已更改为模板,这对我来说是个问题,每次阅读文档以使您保持最新状态是至关重要且可悲的,哈哈 BASE_DIR 指的是什么?谢谢。 嗨杰森。这是 BASE_DIR 的定义。 BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(文件))) @Jeewes,你应该发布一个答案。【参考方案3】:

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

(在我的情况下,它与 allauth 内部的变化有关)

【讨论】:

【参考方案4】:

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

            'django.core.context_processors.csrf',

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

【讨论】:

【参考方案5】:

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

【讨论】:

以上是关于Django:重新启动后出现“没有名为 context_processors 的模块”错误的主要内容,如果未能解决你的问题,请参考以下文章

重新启动或不活动期间后,Django 站点加载速度非常慢

Django + apache & mod_wsgi:更改后必须重新启动 apache

将项目添加到 Django 中的元组元组后是不是可以重新启动服务器?

在 Heroku 上,在实例已经使用更改的模型代码重新启动后,Django syncdb / South 迁移是不是存在危险?

重新安装python后,原来在虚拟环境里的django项目启动报错:dyld: Library not loaded: @executable_path/../.Python Referenced

iOS MagicalRecord 之谜。为啥在 truncateAll 后重新启动时我的数据会重新出现?