Look at a picture First!
1. Request Life cycle
-Wsgi, which is the socket server, is used to receive user requests and to first encapsulate the request, and then hand over the request to the Web framework (Flask, Django)
-Middleware that helps us validate requests or add other related data to the request object, for example: CSRF, request.session
-Route Matching
-View functions, which handle business logic in view functions, may involve: orm, templates = Render
-Middleware to process the data in response.
-Wsgi to send the content of the response to the browser.
2, what Wsgi
Wsgi:web Service Gateway Interface
The module that implements the protocol:
-Wsgiref (beta version, particularly poor performance)
-Werkzurg
-Uwsig
Wsgiref Example:
From Wsgiref.simple_server import make_serverdef run_server (environ, start_response): Start_response (' OK ', [(' Content-type ', ' text/html ')] return [bytes ('
Werkzeug Example:
From werkzeug.wrappers import responsefrom werkzeug.serving import run_simpledef run_server (environ, start_response): Response = response (' Hello ') return response (environ, start_response) #对象if __name__ = = ' __main__ ': run_simple (' 127.0.0.1 ', 8000, run_server)
3. View
URL-function
Url-view
FBV (function base view) is essentially the same as CBV (class base view), except that FBV is based on functions, CBV based on classes. But FBV a few more steps later than CBV.
4, Rest-framework
Rest-framework starts with the dispatch method, executes the view, and executes the rest-framework if there is a rest-framework component .
5. Restfui Specification
View RESTful specification Details:
Django Framework Request life cycle