I couldn't understand how Django handles static files.
I looked it up and summarized it in my own way.
Depending on the site that the location of the static directory refers to
If the design is different, you won't understand ...
STATIC_ROOT
、STATIC_URL
、STATICFILES_DIRS
It wouldn't have been so difficult if I could understand these three meanings ...
settings.py
STATIC_ROOT = os.path.join(BASE_DIR,'static')
Django is on its own when running a local server Because it automatically searches for a static directory directly under each application directory You don't have to be aware of it.
However, settings are required if you want the production web server to manage static files. Because if you want your production web server to manage those static files, It became necessary to collect those files in one place, It seems that the role of this STATIC_ROOT is to decide where to collect it.
So when I typed the $ python manage.py collectstatic
command,
Specify the destination to aggregate the static files of each application as the setting value.
There is no description here by default, so you need to add it yourself.
settings.py
STATIC_URL = '/static/'
A setting value that determines what URL the static file should be published at.
Example: If you say / static /
, each static file will look like domain / static / ????
You will be able to access it.
Unless you are particular about it, it seems that there is no problem with / static / listed by default.
settings.py
STATICFILES_DIRS = (
os.path.join(BASE_DIR, "static"),
)
Django is on its own when running a local server It will automatically search for a static directory directly under each app directory, If otherwise as a static file directory If there is a directory you want to recognize, you can set it with STATICFILES_DIRS. Basically it shouldn't be necessary.
Recommended Posts