Search code examples
pythondjangodjango-settings

Django: PosixPath object is not iterable


Before version 3.x of Django it was possible to define templates, static and media folder using os.path. Into settings.py I had this configuration:

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')]

Now the settings are changed and I need to use Path:

from pathlib import Path
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent

I'm following this new way but I think something is not clear for me. My new settings.py configuration is:

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'

But with that configuration I see this error:

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


Solution

  • Try with this - STATICFILES_DIRS = [BASE_DIR / 'static',]