Django static file handling
Preface
Django's static files can't display pictures by directly placing picture links like html, but pictures, css, and js are collectively referred to as static files.
Configure staticfiles
Open the settings.py of the project and add django.contrib.staticfiles to INSTALLED_APPS:
STATIC_URL
See in settings.py:
STATIC_URL = '/static/'
Here, for the STATIC_URL
set '/static/'
, specifies: Each APP
inside the static resource directory.
In the template, use the static template tag to 相对路径
construct the URL based on the configuration STATICFILES_STORAGE bit .
(That is, the path relative to static in the app)
Directory structure:
mysite/
mysite/
setting.py
...
blog/
static/ # 这个目录,便是我们所设置的 STATIC_URL
blog/
/image
A.jpg
...
manage.py
{
% load static %}
<img src="{% static "blog/image/A.jpg" %}" alt="My image">
STATICFILES_DIRS
Usually static resource files, such as some common .css, .js, *.png, etc., may be used by all apps, such as bootstrap.min.css, copy it in each blog/static/css/ Paste it again?
This is obviously wrong, so STATICFILES_DIRS is used to store these public static resource files. Now let's configure it:
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static')
]
The directory name configured here is still called "static", and it is located in the root directory of the entire Project (in the same directory as manage.py):
mysite/
mysite/
setting.py
...
blog/
static/ # STATIC_URL
blog/
/image
A.jpg
manage.py
static/ # STATICFILES_DIR
/img
A.jpg
Same as STATIC_URL
{
% load static %}
<img src="{% static "img/A.jpg" %}" alt="My image">
STATIC_ROOT
This directory is the biggest difference between Django development mode and production mode.
The static resources are configured before DEBUG = True
, ah, all files are loaded normally!
When you deploy to a production environment DEBUG = False
,, aha, why so many 404s!
First of all, you have to understand that in development mode, Django will find the corresponding static files according to the STATIC related path configuration given, and respond to the client;
In a production environment, Django no longer processes static resource files, you need to let the WEB server handle these static files.
But now, our files are scattered in /mysite/blog/static/, /mysite/static/, if you are using Django admin, then the static files related to admin can still be in the Django pacakage source code!
Therefore, we need to collect these scattered static resource files in a unified location. This location is:STATIC_ROOT
STATIC_ROOT = os.path.join(BASE_DIR, 'static_cdn')
Set up STATIC_ROOT
after the execution collectstatic, all you registered with APP, static files are used to collect your specified STATIC_ROOT
directory:
python manage.py collectstatic
After the execution is completed, a static_cdn/ will be added to the root directory.
Assuming that the WEB server used is Nginx, and we have successfully deployed the Django project.
Since the STATIC_URL set by Django is'/static/', the client will still be the same as in the development mode, and the static resource file request will still follow:
server_name/static/app_name/xxx.css
Therefore, we need to alias the access of /static/ to /static_cdn/:
Restart:
service nginx reload