I have some troubles understanding how static files works on Django 3.0.7
In my settings.py
file, I wrote this:
STATIC_URL = '/static/'
STATICFILES_DIRS = (os.path.join(BASE_DIR, "static"),)
STATIC_ROOT = os.path.join('static')
Here is what I understood. So correct me if I'm wrong. This is the whole point of my question.
STATIC_URL = '/static/'
This one is quite self explanatory. It's the URL root to the static files.
This way I can reach my picture.jpg
in my /static/
folder via my browser by typing the http://<ip-or-domain>/static/picture.jpg
.
But I have some troubles with the two others.
STATICFILES_DIRS = (os.path.join(BASE_DIR, "static"),)
This parameter tells Django I want to use all the folders called static
as static folder where Django will collect every file to put them in the main static folder with the command python3 manage.py collectstatic
. The first argument gives the root where Django should start searching and the second argument is the name of the folder which be identified as app static folder.
STATIC_ROOT = os.path.join('static')
And this parameter is the one which indicate the path of the main static folder where the collected static files will be put.
Am I right until now ?
So here is my issue. When I launch my server, I got this error on my development server, which I don't on my production server:
ERRORS:
?: (staticfiles.E002) The STATICFILES_DIRS setting should not contain the STATIC_ROOT setting.
So this is logic, because the STATICFILES_DIRS
includes the main static folder (at the root of my project). And this one cannot be collected in order to put the files into it again.
But if I remove STATICFILES_DIRS
, Django won't be able to collect the files from each app.
If I remove STATIC_ROOT
, all my static files won't be usable by my app once the collectstatic
done.
What is the problem and can I solve it ? Did I misunderstood something ?
Must I rename my main static folder with another name ? This is quite a strange solution because I saw a lot of Django projects where they still use static
as name for their main static folder as well as the app static folders.
Thank you for helping a Django noob anyway. I read a lot of similar questions on Stackoverflow but I still don't understand where I'm wrong.
FYI:
Django 3.0.7
Python 3.7
(dev) Ubuntu 20.04 LTS
(prod) Ubuntu 18.04 LTS server