【问题标题】:for i, middleware in enumerate(settings.MIDDLEWARE): TypeError: 'NoneType' object is not iterable对于我,枚举中的中间件(settings.MIDDLEWARE):TypeError:'NoneType'对象不可迭代
【发布时间】:2019-07-25 12:50:39
【问题描述】:

我在尝试运行时遇到此错误

python manage.py runserver

或命令行中的任何其他 python manage.py * 命令。

但是当我尝试 python manage.py shell 时,它没有错误地连接并从数据库中获取数据。

错误:

 Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x7fa03b7386a8>
Traceback (most recent call last):
  File "/home/tornike/apps/env/lib/python3.6/site-packages/django/utils/autoreload.py", line 227, in wrapper
    fn(*args, **kwargs)
  File "/home/tornike/apps/env/lib/python3.6/site-packages/django/core/management/commands/runserver.py", line 125, in inner_run
    self.check(display_num_errors=True)
  File "/home/tornike/apps/env/lib/python3.6/site-packages/django/core/management/base.py", line 359, in check
    include_deployment_checks=include_deployment_checks,
  File "/home/tornike/apps/env/lib/python3.6/site-packages/django/core/management/base.py", line 346, in _run_checks
    return checks.run_checks(**kwargs)
  File "/home/tornike/apps/env/lib/python3.6/site-packages/django/core/checks/registry.py", line 81, in run_checks
    new_errors = check(app_configs=app_configs)
  File "/home/tornike/apps/env/lib/python3.6/site-packages/debug_toolbar/apps.py", line 25, in check_middleware
    for i, middleware in enumerate(settings.MIDDLEWARE):
TypeError: 'NoneType' object is not iterable




MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.locale.LocaleMiddleware',
    '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',
    'django.middleware.security.SecurityMiddleware'
)

【问题讨论】:

  • 您是否在settings.py 中设置了MIDDLEWARE 设置?
  • 从错误中可以明显看出settings.MIDDLEWARENone为什么None,从您提供的信息中我们无法判断。它很可能没有在settings.py 中定义
  • @WillemVanOnsem 你什么意思?
  • @DeepSpace 我更新了我的问题,这是我在 settings.py 中的中间件。在另一台电脑上它可以完美运行。
  • @I.Jokhadze:不,这些是MIDDLEWARE_CLASSES。那就是“旧式”。你应该把它重命名为MIDDLEWARE

标签: python django


【解决方案1】:

错误清除显示 settings.MIDDLEWARE 为 None 但您已发布从 Django 2.0 中删除的 MIDDLEWARE_CLASSES。如果 django 的版本 >=2.0,请确保在 settings.py 中使用 MIDDLEWARE。

【讨论】:

    猜你喜欢
    • 2012-08-25
    • 1970-01-01
    • 2017-08-29
    • 2016-08-30
    • 2014-11-21
    • 1970-01-01
    相关资源
    最近更新 更多