I'm using the latest version of Pycharm, 2020.2.2 and django 3.1.
In my project, I removed the default settings.py and created a directory named settings, so the whole project root looks like:
tsetmc
├── asgi.py
├── celery.py
├── context_processors.py
├── __init__.py
├── settings
│ ├── base.py
│ ├── __init__.py
│ ├── local.py
├── urls.py
├── views.py
└── wsgi.py
and in the base.py, I defined the static files setting as:
from pathlib import Path
BASE_DIR = Path(__file__).resolve().parent.parent.parent
...
STATIC_URL = '/static/'
STATICFILES_DIRS = [
BASE_DIR / 'assets/'
]
STATIC_ROOT = BASE_DIR / 'staticfiles/'
MEDIA_URL = '/media/'
MEDIA_ROOT = BASE_DIR / 'media/'
Everything works fine in the browser and the static files are successfully loaded using the {% static %} tag; However, Pycharm can not resolve any of the static files in the templates.
I enabled Django Support, set Django project root and settings in the Pycharm settings accordingly and also set the Template Language as Django; but it didn't solve the issue.
After some trial-and-error, I found an odd solution; If I use import os and os.path.join() to locate the static paths, instead of from pathlib import Path and /, Pycharm can resolve the static files without any problem.
So when I changed my base.py to look like this:
import os
BASE_DIR = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'assets')
]
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles/')
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media/')
(after invalidating the cache, without changing any other configuration)
Pycahrm can fully resolve the static files.
What am I missing here? Is there any problem with using Path to address the static files? or the problem is about the Pycharm itself?
Thanks for the help.
