This article is participating in Python Theme Month. See the link to the event for more details

Rest cannot be enjoyed by lazy people.~~

preface

Django is a Web framework. In previous articles, we’ve derived the implementation of the Python Web framework and seen what happens when you type in a web address and press enter in the client browser. To get a better understanding of how Django works, This article describes what happens in the Django framework when a client browser accesses the Django back end — the request lifecycle of the Django framework. This is a common interview question

WSGI protocol & UWSGI & WSGIREF & Gunicorn

Before introducing the Django request lifecycle process, there are a few concepts you need to understand.

Python Web programs are generally divided into two parts, the server program and the application program. The server program is responsible for the encapsulation of the Socket server and the collation of various data and information requested by the client when the client requests the server. Application is responsible for the specific logic processing, in order to facilitate application development in many web framework, Django is one of them, the server program for different web frameworks provide support, so they need a standard, as long as the server program and the application is the web framework is supported by this standard, Server programs can be used in conjunction with Web frameworks, and WSGI is a specification that specifies the format of the interface between a Web application written in Python and a Web server program.

Common wsGI-compliant server applications include UWSGI and Gunicorn, while the django framework comes with wsGIREF, which can be changed to UWSGI or Gunicorn when a Django project goes live.

Django requests the lifecycle

When a client browser sends a request to the Django back end, the django back end has to go through “a lot of hoops” to get data back to the client browser.

After the client browser sends a request to the Django server, it first passes through the Web gateway interface, which parses the request into HTTP data and encapsulates it into a Request object. The parsed data is sent back to the application. When a request arrives, it passes through the process_request method in each middleware to reach the actual Django back end.

The request will pass through the routing layer and execute the corresponding view function after routing matching. After passing through the business logic of the view function, the response object will be generated. The response object will also return to the Web service gateway interface through each process_response method in the middleware. The Response object is packaged as DATA in HTTP format and returned to the client browser.

The drawing is explained as follows:

conclusion

The article was first published in the wechat public account program Yuan Xiaozhuang, synchronized with nuggets.

Please explain where it came from. If you pass by, please put out your cute little finger and click like before you go (╹▽╹)