1. Categories of Django Static Files
Django static files fall into two categories: static and media.
- static: files referenced by pages, such as JS, CSS, and images
- media: files uploaded by users
2. Production Environment Configuration
In production, you usually configure Nginx to forward static file requests while Django handles dynamic requests.
nginx configuration
| |
When deploying, you need to run the python manage.py collectstatic command to collect the static assets of every Django App in the INSTALLED_APPS list into the directory specified by STATIC_ROOT.
3. Development Environment Configuration
3.1 Configuration and Usage
Step one, add 'django.contrib.staticfiles' to INSTALLED_APPS.
Step two, add the following routes in urls.py. They take effect only when settings.DEBUG==True; in production, Nginx forwards the requests.
| |
Step three, add the following in the settings file under TEMPLATES - OPTIONS - context_processors.
| |
Step four, use the STATIC_URL and MEDIA_URL variables in templates.
static files
| |
media files
| |
3.2 Handling Flow
Step one, Django receives a request for a static file, for example /static/css/main.css.
Step two, Django looks for the file css/main.css in STATICFILES_DIRS.
If step two finds it, that file is returned directly; otherwise, Django keeps looking in the static directory of every Django App in the INSTALLED_APPS list.
4. static-Related Variables
4.1 STATIC_ROOT
Specifies the directory where static files are stored when the python manage.py collectstatic command runs.
4.2 STATIC_URL
The URL mapping, which specifies the URL of the static directory. The default value is:
| |
4.3 STATICFILES_DIRS
STATICFILES_DIRS is a list that specifies which directories in the project hold static files.
4.4 STATICFILES_STORAGE
The file storage engine Django uses when collecting static files with the python manage.py collectstatic command. If the static files need to be hosted somewhere else, you need to change the STATICFILES_STORAGE parameter and implement the corresponding methods. The default value is:
| |
4.5 STATICFILES_FINDERS
In a development environment, the order in which django.contrib.staticfiles looks for static assets depends on the STATICFILES_FINDERS configuration. The default STATICFILES_FINDERS configuration is:
| |
- django.contrib.staticfiles.finders.FileSystemFinder looks for static files in the paths specified by STATICFILES_DIRS
- django.contrib.staticfiles.finders.AppDirectoriesFinder looks for static files in the static directory of every Django App in the INSTALLED_APPS list
5. media-Related Variables
5.1 MEDIA_ROOT
MEDIA stores files uploaded by users, such as the files of a FileField in a Model. If MEDIA_ROOT=C:\media is defined, then with File = models.FileField(upload_to='file/') the uploaded file is saved to C:\media\file.
MEDIA_ROOT set in settings must be an absolute path. You can write it like this:
| |
5.2 MEDIA_URL
MEDIA_URL is the URL prefix used when accessing from a browser, for example:
| |
When the browser visits http://localhost/mymedia/1.png, it is accessing c:\media\1.png.
