Learning history for participating in team app development in Python ~ Django Tutorial 6 ~

Introduction

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.

Manage static files with Django

What are static and dynamic files in the first place?

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

Main subject

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

Reference: What is a web server Reference: Works well with static files in Django Django documentation

Recommended Posts

Learning history for participating in team app development in Python ~ Django Tutorial 5 ~
Learning history for participating in team app development in Python ~ Django Tutorial 4 ~
Learning history for participating in team app development in Python ~ Django Tutorial 1, 2, 3 ~
Learning history for participating in team app development in Python ~ Django Tutorial 6 ~
Learning history for participating in team app development in Python ~ Django Tutorial 7 ~
Learning history for participating in team application development in Python ~ Think a little about requirement definition ~
Learning history for participating in team application development in Python ~ Supplement of basic items and construction of jupyterLab environment ~
Learning history to participate in team application development with Python ~ Build Docker / Django / Nginx / MariaDB environment ~
Learning history to participate in team application development in Python ~ After finishing "Introduction to Python 3" of paiza learning ~
Python Django Tutorial (5)
Python Django Tutorial (2)
Python Django Tutorial (8)
Python Django Tutorial (6)
Python Django Tutorial (7)
Python Django Tutorial (1)
Python Django tutorial tutorial
Python Django Tutorial (3)
Python Django Tutorial (4)
Boost.NumPy Tutorial for Extending Python in C ++ (Practice)
[Implementation for learning] Implement Stratified Sampling in Python (1)
Learning notes for the migrations feature in the Django framework (2)
Build an interactive environment for machine learning in Python
Directory structure for test-driven development using pytest in python
App development to tweet in Python from Visual Studio 2017
Learning notes for the migrations feature in the Django framework (1)
Deep Learning Experienced in Python Chapter 2 (Materials for Journals)
Framework development in Python
Development environment in Python
AWS SDK for Python (Boto3) development in Visual Studio 2017
Slackbot development in Python
Tutorial for doing Test Driven Development (TDD) in Flask-2 Decorators
Building a development environment for Android apps-creating Android apps in Python
Tutorial for doing Test Driven Development (TDD) in Flask ―― 1 Test Client
Learning flow for Python beginners
Python learning plan for AI learning
Search for strings in Python
Techniques for sorting in Python
Python development in Visual Studio 2017
Qt for Python app self-update
Python Django Tutorial Cheat Sheet
Checkio's recommendation for learning Python
[For organizing] Python development environment
Python development in Visual Studio
About "for _ in range ():" in python
How about Anaconda for building a machine learning environment in Python?
Automatically resize screenshots for the App Store for each screen in Python
EEG analysis in Python: Python MNE tutorial
Implement stacking learning in Python [Kaggle]
Check for memory leaks in Python
8 Frequently Used Commands in Python Django
Web teaching materials for learning Python
Web application development memo in python
[For beginners] Django -Development environment construction-
Widrow-Hoff learning rules implemented in Python
Python development environment options for May 2020
Emacs settings for Python development environment
Python: Preprocessing in Machine Learning: Overview
Implemented Perceptron learning rules in Python
Run unittests in Python (for beginners)
Prepare Python development environment for each project in Windows environment (VSCode + virtualEnvWrapper + Pylint)
Development of MTG card evaluation posting site ~ Django app release in 5 weeks ~