When I built the environment with this article, I created static
directly under the root as a management folder for static files.
And in settings.py
STATIC_URL = '/static/'
STATIC_ROOT = '/static'
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR,'media')
Do you remember making these settings?
And do you remember finally casting the python manage.py collectstatic
spell like this?
This time, I would like to delve a little deeper into what the flow is like around here and reorganize it.
I will organize it again.
--I think you've understood before that static files are files that are exchanged without being changed or updated between exchanges, but more strictly speaking, from the server to the browser within a static web server. A file that is processed so that it is sent "as is". The process of returning the requested file in the server as an HTTP response to the browser is performed.
--On the other hand, a dynamic file is a server consisting of a static web server, application server, database, etc., and refers to a file that is updated before the application server sends it to the browser through the HTTP server. I will. In the article at the beginning, the login process is summarized as an example, but when a request comes from the browser, the request is passed to the application server through the HTTP server etc. in the Web server, and data is acquired from the DB based on it. In addition, processing such as generating a response according to the request from there is performed. It seems that this kind of processing is called "dynamic".
Reference: What is a web server Reference: Works well with static files in Django
Let's return to the beginning.
Django's handling of static files depends on whether settings.DEBUG
is True
or False
. And that seems to be a confusing place.
First of all, if it is True
, that is, in the development mode, you can specify it thanks to django.contrib.staticfiles
, and if you execute the runserver
command, the static files will be applied by default.
Then False
, that is, what happens in the production environment, if you do not set anything, the static file will be ignored.
The reason is that in a production environment, unlike a local environment, a web server such as Nginx manages the exchange of static files.
Once you understand this, let's look back on the following settings in the opening article.
nginx.conf
upstream django {
ip_hash;
server python:8888;
}
# configuration of the server(Server configuration)
server {
# the port your site will be served on
listen 8000;
# the domain name it will serve for
server_name 127.0.0.1; # substitute your machine`s IP address or FQDN
charset utf-8;
# max upload size(Media file upload limit)
client_max_body_size 75M;
location /static {
alias /static;
}
# Finally, send all non-media requests to the Django server.
location / {
uwsgi_pass django;
include /etc/nginx/uwsgi_params; # the uwsgi_params file you installed
}
}
This was a file that set how the request should be passed in Nginx. To review
--The request is first caught on port 8000. --If what you catch is a request to a static file, pass it to Nginx static. --Otherwise, if you need to pass the request to the application server (uWSGI), skip the request to port 8888.
It is written that. Let's take a look at this as well.
settings.py
STATIC_URL = '/static/'
STATIC_ROOT = '/static'
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR,'media')
STATIC_URL
sets the URL that will be hit when you want to reference a static file in your Django application.
Here and in the location / static {alias / static;}
part on the Nginx side, the route for exchanging static files on the Web server side and Django side is set.
STATIC_ROOT ='/ static'
sets the directory to the folder where the static files are collected.
In the article at the beginning and this tutorial, static
is created as a folder for that purpose directly under the root, but in fact, static can be placed directly under each application folder.
However, this would complicate management, so create a folder to organize static folders, and try to store the static file folder for each application in it.
Finally, to apply the static files to admin, the admin page, run the collect static
command, which aggregates the static files into STATIC_ROOT
, and you're ready to work with the static files in production. ..
The media file settings set by MEDIA_URL
and MEDIA_BOOT
are the same as those of static files, so they will be omitted.
Reference: What is a web server Reference: Works well with static files in Django Django documentation
Recommended Posts