我正在尝试学习django框架,并在尝试运行服务器时遇到此错误。有人能指出我正确的方向吗?在运行python manage.py collectstatic并创建模板之前,一切工作正常。
似乎我在导入模块错误时遇到错误,但是我不确定要看什么。
Django settings for btre project.
Generated by 'django-admin startproject' using Django 2.1.3.
For more information on this file, see
https://docs.djangoproject.com/en/2.1/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/2.1/ref/settings/
"""
import os
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/2.1/howto/deployment/checklist/
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = []
# Application definition
INSTALLED_APPS = [
'pages.apps.PagesConfig',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewnoMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
ROOT_URLCONF = 'btre.urls'
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',
],
},
},
]
WSGI_APPLICATION = 'btre.wsgi.application'
# Database
# https://docs.djangoproject.com/en/2.1/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
# Password validation
# https://docs.djangoproject.com/en/2.1/ref/settings/#auth-password-validators
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]
# Internationalization
# https://docs.djangoproject.com/en/2.1/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/2.1/howto/static-files/
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATIC_URL = '/static/'
STATICFILES_DIRS =[
os.path.join(BASE_DIR, 'btre/static')
]
错误日志:
(venv) Rizwans-MBP:btre_project rizwanrenesa$ python manage.py runserver
Performing system checks...
System check identified no issues (0 silenced).
You have 15 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.
November 22, 2018 - 08:02:36
Django version 2.1.3, using settings 'btre.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x106f89158>
Traceback (most recent call last):
File "/Users/rizwanrenesa/Desktop/btre_project/venv/lib/python3.7/site-packages/django/utils/module_loading.py", line 20, in import_string
return getattr(module, class_name)
AttributeError: module 'django.middleware.csrf' has no attribute 'CsrfViewnoMiddleware'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/Users/rizwanrenesa/Desktop/btre_project/venv/lib/python3.7/site-packages/django/core/servers/basehttp.py", line 44, in get_internal_wsgi_application
return import_string(app_path)
File "/Users/rizwanrenesa/Desktop/btre_project/venv/lib/python3.7/site-packages/django/utils/module_loading.py", line 17, in import_string
module = import_module(module_path)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 728, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/Users/rizwanrenesa/Desktop/btre_project/btre/wsgi.py", line 16, in <module>
application = get_wsgi_application()
File "/Users/rizwanrenesa/Desktop/btre_project/venv/lib/python3.7/site-packages/django/core/wsgi.py", line 13, in get_wsgi_application
return WSGIHandler()
File "/Users/rizwanrenesa/Desktop/btre_project/venv/lib/python3.7/site-packages/django/core/handlers/wsgi.py", line 136, in __init__
self.load_middleware()
File "/Users/rizwanrenesa/Desktop/btre_project/venv/lib/python3.7/site-packages/django/core/handlers/base.py", line 34, in load_middleware
middleware = import_string(middleware_path)
File "/Users/rizwanrenesa/Desktop/btre_project/venv/lib/python3.7/site-packages/django/utils/module_loading.py", line 24, in import_string
) from err
ImportError: Module "django.middleware.csrf" does not define a "CsrfViewnoMiddleware" attribute/class
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/Users/rizwanrenesa/Desktop/btre_project/venv/lib/python3.7/site-packages/django/utils/autoreload.py", line 225, in wrapper
fn(*args, **kwargs)
File "/Users/rizwanrenesa/Desktop/btre_project/venv/lib/python3.7/site-packages/django/core/management/commands/runserver.py", line 137, in inner_run
handler = self.get_handler(*args, **options)
File "/Users/rizwanrenesa/Desktop/btre_project/venv/lib/python3.7/site-packages/django/contrib/staticfiles/management/commands/runserver.py", line 27, in get_handler
handler = super().get_handler(*args, **options)
File "/Users/rizwanrenesa/Desktop/btre_project/venv/lib/python3.7/site-packages/django/core/management/commands/runserver.py", line 64, in get_handler
return get_internal_wsgi_application()
File "/Users/rizwanrenesa/Desktop/btre_project/venv/lib/python3.7/site-packages/django/core/servers/basehttp.py", line 49, in get_internal_wsgi_application
) from err
django.core.exceptions.ImproperlyConfigured: WSGI application 'btre.wsgi.application' could not be loaded; Error importing module.
^Z
[7]+ Stopped python manage.py runserver
(venv) Rizwans-MBP:btre_project rizwanrenesa$ python manage.py runserver
Performing system checks...
System check identified no issues (0 silenced).
You have 15 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.
November 22, 2018 - 08:02:52
Django version 2.1.3, using settings 'btre.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x10e0a6158>
Traceback (most recent call last):
File "/Users/rizwanrenesa/Desktop/btre_project/venv/lib/python3.7/site-packages/django/utils/module_loading.py", line 20, in import_string
return getattr(module, class_name)
AttributeError: module 'django.middleware.csrf' has no attribute 'CsrfViewnoMiddleware'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/Users/rizwanrenesa/Desktop/btre_project/venv/lib/python3.7/site-packages/django/core/servers/basehttp.py", line 44, in get_internal_wsgi_application
return import_string(app_path)
File "/Users/rizwanrenesa/Desktop/btre_project/venv/lib/python3.7/site-packages/django/utils/module_loading.py", line 17, in import_string
module = import_module(module_path)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 728, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/Users/rizwanrenesa/Desktop/btre_project/btre/wsgi.py", line 16, in <module>
application = get_wsgi_application()
File "/Users/rizwanrenesa/Desktop/btre_project/venv/lib/python3.7/site-packages/django/core/wsgi.py", line 13, in get_wsgi_application
return WSGIHandler()
File "/Users/rizwanrenesa/Desktop/btre_project/venv/lib/python3.7/site-packages/django/core/handlers/wsgi.py", line 136, in __init__
self.load_middleware()
File "/Users/rizwanrenesa/Desktop/btre_project/venv/lib/python3.7/site-packages/django/core/handlers/base.py", line 34, in load_middleware
middleware = import_string(middleware_path)
File "/Users/rizwanrenesa/Desktop/btre_project/venv/lib/python3.7/site-packages/django/utils/module_loading.py", line 24, in import_string
) from err
ImportError: Module "django.middleware.csrf" does not define a "CsrfViewnoMiddleware" attribute/class
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/Users/rizwanrenesa/Desktop/btre_project/venv/lib/python3.7/site-packages/django/utils/autoreload.py", line 225, in wrapper
fn(*args, **kwargs)
File "/Users/rizwanrenesa/Desktop/btre_project/venv/lib/python3.7/site-packages/django/core/management/commands/runserver.py", line 137, in inner_run
handler = self.get_handler(*args, **options)
File "/Users/rizwanrenesa/Desktop/btre_project/venv/lib/python3.7/site-packages/django/contrib/staticfiles/management/commands/runserver.py", line 27, in get_handler
handler = super().get_handler(*args, **options)
File "/Users/rizwanrenesa/Desktop/btre_project/venv/lib/python3.7/site-packages/django/core/management/commands/runserver.py", line 64, in get_handler
return get_internal_wsgi_application()
File "/Users/rizwanrenesa/Desktop/btre_project/venv/lib/python3.7/site-packages/django/core/servers/basehttp.py", line 49, in get_internal_wsgi_application
) from err
django.core.exceptions.ImproperlyConfigured: WSGI application 'btre.wsgi.application' could not be loaded; Error importing module.
答案 0 :(得分:2)
以某种方式在内置中间件类名称之一的中间添加了“ no”:“ CsrfViewnoMiddleware”应为“ CsrfViewMiddleware”。