About handling Django static files

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_ROOTSTATIC_URL STATICFILES_DIRS It wouldn't have been so difficult if I could understand these three meanings ...

About STATIC_ROOT

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.

About STATIC_URL

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.

About STATICFILES_DIRS

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

About handling Django static files
[Django] About static file related settings (images, videos)
[Django] About static file related settings (css, js)
Upload files with Django
About tweepy error handling
Python, about exception handling
[Day 6] Handle static files
How to reference static files in a Django project
About django custom filter arguments
Linux (about files and directories)
About FastAPI ~ Endpoint error handling ~
How to handle static files when deploying to production with Django
Data handling 3 (development) About data format
About LINUX files and processes
Handle csv files with Django (django-import-export)
Manage Django config files with Python-decouple
Serve static files with X-Send File
Django static file (static) related settings summary
[Django] About one-to-many relationships (related_name, _set.all ())
How to display Django static files correctly under Amazon Linux2 + Apache environment