The HTTP processing process of Django is roughly the same as that of other web frameworks. Django processes a request through middleware and then by default URL. We can intercept all requests in the middleware, and directly return response after processing in our own way.
1. load configuration
Django configurations are in "Project/settings. "py" can be the configuration of Django, or custom configuration. conf. settings access is very convenient.
2. Start
The core action is through Django. core. management. commands. runfcgi command to start, it runs Django. core. servers. in FastCGI, runfastcgi uses the flup wsgiserver to start FastCGI. Wsgiserver carries Django. core. handlers. an instance of the wsgihandler class of wsgi uses wsgihandler to handle requests sent by the Web server for reasons (such as Apache and Lighttpd). At this point, the request truly enters the Django world.
3. process the request
When an HTTP request comes, wsgihandler starts to work, which is inherited from basehandler. Wsgihandler creates a wsgirequest instance for each request, and wsgirequest is inherited from HTTP. httprequest. Next, create response.
4. Create response
The GET_RESPONSE method of basehandler is to create a response based on the request, and the specific action to generate a response is to execute URLs. view function in Py, which is also a key step for Django to process "friendly URL". Each such function returns a response instance. In this case, the general practice is to load the template through loader and generate the page content. The important thing is to extract data from the database through the ORM technology and render the data to the template, to generate a specific page.
5. Process Response
Django returns response to flup, and flup retrieves the response content and returns it to the Web server, which then returns it to the browser.
In short, Django mainly implements two things in FastCGI: processing requests and creating response, and their core tasks are "URLs analysis", "template technology", and "ORM technology ".
An HTTP request is first converted into an httprequest object,
The object is then passed to the request middleware for processing. If the middleware returns response, it is directly passed to the response middleware for final processing.
Otherwise, the request middleware will access the URL configuration, determine which view to process, and determine which view to execute,
However, when the view is not executed, the system will pass the request to the view middleware processor for processing,
If the middleware returns response, the response is directly passed to the response middleware for subsequent processing,
Otherwise, a definite view function will be executed and response will be returned. If an exception is thrown during this process, it will be processed by the exception middleware processor.
[Django] request processing process (transfer)