Python ValueError:Django框架中的值不足,无法解包(预期为2,实际为1)
我是django的初学者,我刚刚开始学习如何加载静态文件和所有关于静态文件的知识。在运行服务器时设置整个代码后,我遇到以下错误:Python ValueError:Django框架中的值不足,无法解包(预期为2,实际为1),python,django,django-staticfiles,Python,Django,Django Staticfiles,我是django的初学者,我刚刚开始学习如何加载静态文件和所有关于静态文件的知识。在运行服务器时设置整个代码后,我遇到以下错误: `C:\Users\Lenovo\Documents\Django project\project3\fees\views.py changed, reloading. Watching for file changes with StatReloader Performing system checks... Exception in thread django-
`C:\Users\Lenovo\Documents\Django project\project3\fees\views.py changed, reloading.
Watching for file changes with StatReloader
Performing system checks...
Exception in thread django-main-thread:
Traceback (most recent call last):
File "C:\Users\Lenovo\anaconda3\lib\threading.py", line 932, in _bootstrap_inner
self.run()
File "C:\Users\Lenovo\anaconda3\lib\threading.py", line 870, in run
self._target(*self._args, **self._kwargs)
File "C:\Users\Lenovo\anaconda3\lib\site-packages\django\utils\autoreload.py", line 53, in wrapper
fn(*args, **kwargs)
File "C:\Users\Lenovo\anaconda3\lib\site-packages\django\core\management\commands\runserver.py", line 118, in inner_run
self.check(display_num_errors=True)
File "C:\Users\Lenovo\anaconda3\lib\site-packages\django\core\management\base.py", line 392, in check
all_issues = checks.run_checks(
File "C:\Users\Lenovo\anaconda3\lib\site-packages\django\core\checks\registry.py", line 70, in run_checks
new_errors = check(app_configs=app_configs, databases=databases)
File "C:\Users\Lenovo\anaconda3\lib\site-packages\django\contrib\staticfiles\checks.py", line 7, in check_finders
for finder in get_finders():
File "C:\Users\Lenovo\anaconda3\lib\site-packages\django\contrib\staticfiles\finders.py", line 282, in get_finders
yield get_finder(finder_path)
File "C:\Users\Lenovo\anaconda3\lib\site-packages\django\contrib\staticfiles\finders.py", line 295, in get_finder
return Finder()
File "C:\Users\Lenovo\anaconda3\lib\site-packages\django\contrib\staticfiles\finders.py", line 59, in __init__
prefix, root = root
ValueError: not enough values to unpack (expected 2, got 1)`
请告诉我为什么会出现这个错误,以及如何解决它
下面是seting.py文件
"""
Django settings for project3 project.
Generated by 'django-admin startproject' using Django 3.1.1.
For more information on this file, see
https://docs.djangoproject.com/en/3.1/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/3.1/ref/settings/
"""
from pathlib import Path
import os
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
TEMPLATE_DIRS = [os.path.join(BASE_DIR / 'templates'),]
STATIC_DIRS = [os.path.join(BASE_DIR / 'static'),]
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'course',
'fees',
]
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': TEMPLATE_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',
],
},
},
]
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.1/howto/static-files/
STATIC_URL = '/static/'
STATICFILES_DIRS = [STATIC_DIRS,]
换成
prefix = root
或
Django 3.1不再使用
os
在settings.py
中连接路径。它使用pathlib,它提供了一个路径
对象
BASE_DIR = Path(__file__).resolve().parent.parent
然后只能用斜线连接:
TEMPLATE_DIRS = [BASE_DIR / 'templates',]
STATIC_DIRS = [BASE_DIR / 'static',]
根本不需要导入操作系统
,然后
我发现,finders.py
中的代码期望使用/
连接pathlib的两个部分,然后使用os.join
以某种方式组合它们,这将它减少为一个变量,因此缺少值。这最后一点是一种猜测,虽然-我还没有测试,这正是正在发生的事情。但是按照我的建议修改你的代码,我认为它会起作用
这就是我在3.1中当前项目中配置内容的方式
TEMPLATE_DIR = BASE_DIR / "templates"
TEMPLATES = [
{
"BACKEND": "django.template.backends.django.DjangoTemplates",
"DIRS": [
TEMPLATE_DIR,
],
...
}
...
STATIC_URL = "/static/"
STATICFILES_DIRS = (BASE_DIR / "static",)
很确定问题不在Django本身的
finders.py
中。。。更重要的是,它得到了一些不应该像os
尝试加入pathlib
路径那样的东西。。。我不太清楚这会是什么样子,但我猜前缀/根然后被os.join
合并到一个字段中,这会产生错误(我承认这是毫无帮助的)…它也出现在注释#项目内部的构建路径中,如下所示:BASE_DIR/'subdir.
在操作代码中,但是谁读评论…:-)@urbanespaceman我写的代码与答案中给出的代码相同,但仍然得到相同的错误。@urbanespaceman确实需要在最新的STATIC\u URL='/STATIC/'STATICFILES\u DIRS=[STATIC\u DIRS,]
我将添加一些示例,说明如何在我的一个项目中设置这些…@urbanespaceman非常感谢您,现在它工作正常了
TEMPLATE_DIRS = [BASE_DIR / 'templates',]
STATIC_DIRS = [BASE_DIR / 'static',]
TEMPLATE_DIR = BASE_DIR / "templates"
TEMPLATES = [
{
"BACKEND": "django.template.backends.django.DjangoTemplates",
"DIRS": [
TEMPLATE_DIR,
],
...
}
...
STATIC_URL = "/static/"
STATICFILES_DIRS = (BASE_DIR / "static",)