Django:PosixPath 对象不可迭代
Django: PosixPath object is not iterable
在 Django 版本 3.x 之前,可以使用 os.path
定义模板、静态和媒体文件夹。进入 settings.py
我有这样的配置:
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
.
.
.
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')],
.
.
.
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), 'static-folder')
MEDIA_ROOT = os.path.join(os.path.dirname(BASE_DIR), 'media-folder')
MEDIA_URL = '/media/'
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static')]
现在更改了设置,我需要使用Path
:
from pathlib import Path
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
我正在采用这种新方式,但我觉得有些事情对我来说还不清楚。我的新 settings.py
配置是:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
BASE_DIR / 'templates',
],
.
.
.
STATIC_URL = '/static/'
STATIC_ROOT = BASE_DIR / 'static-folder'
MEDIA_URL = '/media/'
MEDIA_ROOT = BASE_DIR / 'media-folder'
STATICFILES_DIRS = BASE_DIR / 'static'
但是使用那个配置我看到这个错误:
Exception in thread django-main-thread: Traceback (most recent call
last): File "/usr/lib/python3.8/threading.py", line 932, in
_bootstrap_inner
self.run() File "/usr/lib/python3.8/threading.py", line 870, in run
self._target(*self._args, **self._kwargs) File "/home/maxdragonheart/DEV_FOLDER/MIO/Miosito/WebApp/backend/.env/lib/python3.8/site-packages/django/utils/autoreload.py",
line 53, in wrapper
fn(*args, **kwargs) File "/home/maxdragonheart/DEV_FOLDER/MIO/Miosito/WebApp/backend/.env/lib/python3.8/site-packages/django/core/management/commands/runserver.py",
line 118, in inner_run
self.check(display_num_errors=True) File "/home/maxdragonheart/DEV_FOLDER/MIO/Miosito/WebApp/backend/.env/lib/python3.8/site-packages/django/core/management/base.py",
line 392, in check
all_issues = checks.run_checks( File "/home/maxdragonheart/DEV_FOLDER/MIO/Miosito/WebApp/backend/.env/lib/python3.8/site-packages/django/core/checks/registry.py",
line 70, in run_checks
new_errors = check(app_configs=app_configs, databases=databases) File
"/home/maxdragonheart/DEV_FOLDER/MIO/Miosito/WebApp/backend/.env/lib/python3.8/site-packages/django/contrib/staticfiles/checks.py",
line 7, in check_finders
for finder in get_finders(): File "/home/maxdragonheart/DEV_FOLDER/MIO/Miosito/WebApp/backend/.env/lib/python3.8/site-packages/django/contrib/staticfiles/finders.py",
line 282, in get_finders
yield get_finder(finder_path) File "/home/maxdragonheart/DEV_FOLDER/MIO/Miosito/WebApp/backend/.env/lib/python3.8/site-packages/django/contrib/staticfiles/finders.py",
line 295, in get_finder
return Finder() File "/home/maxdragonheart/DEV_FOLDER/MIO/Miosito/WebApp/backend/.env/lib/python3.8/site-packages/django/contrib/staticfiles/finders.py",
line 57, in init
for root in settings.STATICFILES_DIRS: TypeError: 'PosixPath' object is not iterable
试试这个 - STATICFILES_DIRS = [BASE_DIR / 'static',]
在 Django 版本 3.x 之前,可以使用 os.path
定义模板、静态和媒体文件夹。进入 settings.py
我有这样的配置:
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
.
.
.
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')],
.
.
.
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), 'static-folder')
MEDIA_ROOT = os.path.join(os.path.dirname(BASE_DIR), 'media-folder')
MEDIA_URL = '/media/'
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static')]
现在更改了设置,我需要使用Path
:
from pathlib import Path
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
我正在采用这种新方式,但我觉得有些事情对我来说还不清楚。我的新 settings.py
配置是:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
BASE_DIR / 'templates',
],
.
.
.
STATIC_URL = '/static/'
STATIC_ROOT = BASE_DIR / 'static-folder'
MEDIA_URL = '/media/'
MEDIA_ROOT = BASE_DIR / 'media-folder'
STATICFILES_DIRS = BASE_DIR / 'static'
但是使用那个配置我看到这个错误:
Exception in thread django-main-thread: Traceback (most recent call last): File "/usr/lib/python3.8/threading.py", line 932, in _bootstrap_inner self.run() File "/usr/lib/python3.8/threading.py", line 870, in run self._target(*self._args, **self._kwargs) File "/home/maxdragonheart/DEV_FOLDER/MIO/Miosito/WebApp/backend/.env/lib/python3.8/site-packages/django/utils/autoreload.py", line 53, in wrapper fn(*args, **kwargs) File "/home/maxdragonheart/DEV_FOLDER/MIO/Miosito/WebApp/backend/.env/lib/python3.8/site-packages/django/core/management/commands/runserver.py", line 118, in inner_run self.check(display_num_errors=True) File "/home/maxdragonheart/DEV_FOLDER/MIO/Miosito/WebApp/backend/.env/lib/python3.8/site-packages/django/core/management/base.py", line 392, in check all_issues = checks.run_checks( File "/home/maxdragonheart/DEV_FOLDER/MIO/Miosito/WebApp/backend/.env/lib/python3.8/site-packages/django/core/checks/registry.py", line 70, in run_checks new_errors = check(app_configs=app_configs, databases=databases) File "/home/maxdragonheart/DEV_FOLDER/MIO/Miosito/WebApp/backend/.env/lib/python3.8/site-packages/django/contrib/staticfiles/checks.py", line 7, in check_finders for finder in get_finders(): File "/home/maxdragonheart/DEV_FOLDER/MIO/Miosito/WebApp/backend/.env/lib/python3.8/site-packages/django/contrib/staticfiles/finders.py", line 282, in get_finders yield get_finder(finder_path) File "/home/maxdragonheart/DEV_FOLDER/MIO/Miosito/WebApp/backend/.env/lib/python3.8/site-packages/django/contrib/staticfiles/finders.py", line 295, in get_finder return Finder() File "/home/maxdragonheart/DEV_FOLDER/MIO/Miosito/WebApp/backend/.env/lib/python3.8/site-packages/django/contrib/staticfiles/finders.py", line 57, in init for root in settings.STATICFILES_DIRS: TypeError: 'PosixPath' object is not iterable
试试这个 - STATICFILES_DIRS = [BASE_DIR / 'static',]