我安装了其余框架,现在尝试通过将其添加到INSTALLED_APPS
文件的settings.py
部分来将其添加到我的Django项目中,但是每当我尝试在终端中获取它时,运行我的服务器。
直到我将'rest-framework'
添加到settings.py
为止,后端完全可用。
在寻找解决方案但找不到任何帮助时,我将不胜感激
Watching for file changes with StatReloader
Performing system checks...
Exception in thread django-main-thread:
Traceback (most recent call last):
File "c:\users\admin\django\django\template\utils.py", line 66, in __getitem__
return self._engines[alias]
KeyError: 'django'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "c:\users\admin\django\django\template\backends\django.py",
line 121, in get_package_libraries
module = import_module(entry[1])
File "C:\Users\Admin\AppData\Local\Programs\Python\Python37-32\lib\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 "C:\Users\Admin\AppData\Local\Programs\Python\Python37-32\lib\site-packages\rest_framework\templatetags\rest_framework.py", line 9, in <module>
from django.utils import six
ImportError: cannot import name 'six' from 'django.utils' (c:\users\admin\django\django\utils\__init__.py)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\Admin\AppData\Local\Programs\Python\Python37-32\lib\threading.py", line 917, in _bootstrap_inner
self.run()
File "C:\Users\Admin\AppData\Local\Programs\Python\Python37-32\lib\threading.py", line 865, in run
self._target(*self._args, **self._kwargs)
File "c:\users\admin\django\django\utils\autoreload.py", line 54, in wrapper
fn(*args, **kwargs)
File "c:\users\admin\django\django\core\management\commands\runserver.py", line 117, in inner_run
self.check(display_num_errors=True)
File "c:\users\admin\django\django\core\management\base.py", line 395, in check
include_deployment_checks=include_deployment_checks,
File "c:\users\admin\django\django\core\management\base.py", line 382, in _run_checks
return checks.run_checks(**kwargs)
File "c:\users\admin\django\django\core\checks\registry.py", line 72, in run_checks
new_errors = check(app_configs=app_configs)
File "c:\users\admin\django\django\contrib\admin\checks.py", line 76, in check_dependencies
for engine in engines.all():
File "c:\users\admin\django\django\template\utils.py", line 90, in all
return [self[alias] for alias in self]
File "c:\users\admin\django\django\template\utils.py", line 90, in <listcomp>
return [self[alias] for alias in self]
File "c:\users\admin\django\django\template\utils.py", line 81, in __getitem__
engine = engine_cls(params)
File "c:\users\admin\django\django\template\backends\django.py",
line 25, in __init__
options['libraries'] = self.get_templatetag_libraries(libraries)
File "c:\users\admin\django\django\template\backends\django.py",
line 43, in get_templatetag_libraries
libraries = get_installed_libraries()
File "c:\users\admin\django\django\template\backends\django.py",
line 108, in get_installed_libraries
for name in get_package_libraries(pkg):
File "c:\users\admin\django\django\template\backends\django.py",
line 125, in get_package_libraries
"trying to load '%s': %s" % (entry[1], e)
django.template.library.InvalidTemplateLibrary: Invalid template library specified. ImportError raised when trying to load 'rest_framework.templatetags.rest_framework': cannot import name 'six' from 'django.utils' (c:\users\admin\django\django\utils\__init__.py)
这是设置。py
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/dev/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 's@q-i0&o-#cwoil9jv$3*e78lscfezur^v3w@vzpu61#56+3k#'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = []
# Application definition
INSTALLED_APPS = [
'music.apps.MusicConfig',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'rest_framework',
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
ROOT_URLCONF = 'website.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'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 = 'website.wsgi.application'
# Database
# https://docs.djangoproject.com/en/dev/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/dev/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/dev/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/dev/howto/static-files/
STATIC_URL = '/static/'
答案 0 :(得分:0)
使用PIP
运行以下命令:
pip install djangorestframework
浏览错误日志后,我认为这里还缺少six
:
pip install six
我希望对您有帮助
答案 1 :(得分:0)
rest_framework
试图从six
导入django.utils
,但在Django 3.0 及更高版本中具有since been removed。
File "C:\Users\Admin\AppData\Local\Programs\Python\Python37-32\lib\site-packages\rest_framework\templatetags\rest_framework.py", line 9, in <module>
from django.utils import six
您可能想将Django版本降级到2.2,或者通过以下方式创建补丁
在django.utils
中创建一个新文件 six.py 并导入six
:
import six
并在您的环境中安装six
:
pip install six