Django Six methods are preset in the middleware , The difference between these six methods is that they are executed in different stages , To intervene in an input or output , The method is as follows :
1. initialization : No parameters required , Called once when the server responds to the first request , Used to determine if the current middleware is enabled
def __init__():
pass
2. Before processing the request : Call... On each request , return None or HttpResponse object .
def process_request(request):
pass
3. Before working with views : Call... On each request , return None or HttpResponse object .
def process_view(request,view_func,view_args,view_kwargs):
pass
4. Before processing the template response : Call... On each request , Back to implementation render Method .
def process_template_response(request,response):
pass
5. After processing the response : All responses are called before returning to the browser , Call... On each request , return HttpResponse object .
def process_response(request,response):
pass
6. exception handling : Called when the view throws an exception , Call... On each request , Return to one HttpResponse object .
def process_exception(request,exception):
pass
Generally, the user sends a request to our server through the browser (request), This request will access the view function , If there is no data call involved , At this time, the view function returns a template, that is, a web page to the user ; View function calls model class to search data in database , The view function fills the returned data into the space in the template , Finally return the web page to the user .
1.wsgi , Request sealed and handed over to web frame (Flask,Django)
2. middleware , Verify the request or add other relevant data to the request object , for example :csrf,request.session
3. Route matching Depending on the browser url To match different view functions
4. The view function , Processing business logic in view function , May involve :orm,templates
5. middleware , Process the response data
6.wsgi, Send the content of the response to the browser
Session The solution is to keep state on the server side , and Cookie The solution is to keep the state on the client side . But it's forbidden Cookie You can't get Session. because Session Yes, it is Session ID To determine the server for the current session Session, and Session ID It's through Cookie To deliver , Ban Cookie amount to SessionID, You don't get Session.
1.Django The time-consuming tasks in are executed by a process or thread , E-mail , Use celery.
2. Deploy django The project is time , The configuration file sets the related configuration of processes and processes .
1.uWSGI It's a Web The server , It has achieved WSGI agreement 、uwsgi、http Such agreement . It's a Web The server ( Such as nginx,uWSGI Wait for the server ) And web application ( If used Flask The program written by the framework ) A specification of communication .
it is to be noted that WSGI/uwsgi/uWSGI The distinction between these three concepts .
WSGI Is a communication protocol .
Why uWSGI Why do we still need nginx?
because nginx Excellent static content processing ability , Then forward the dynamic content to uWSGI The server , This can achieve good client response .
nginx Is an open source high-performance HTTP Servers and reverse agents :
1. As web The server , It handles static files and index files very effectively
2. Its design pays great attention to efficiency , The biggest support 5 Million concurrent connections , But it only takes up very little memory space
3. High stability , Simple configuration .
4. Powerful reverse proxy and load balancing functions , Balance the load pressure of each server in the cluster
class getCurrenttime(APIView):
def get(self,request):
local_time = time.localtime()
time_zone =settings.TIME_ZONE
temp = {
'localtime':local_time,'timezone':time_zone}
return Response(temp)
1、 Django Go in a big and comprehensive direction , High development efficiency . its MTV frame , Self contained ORM,admin Background management , Self contained sqlite Server for database and development testing , Improve the development efficiency of developers . heavyweight web frame , The function is all ready , Provide a one-stop solution , So that developers don't have to spend a lot of time on choice .
Bring their own ORM And template engine , Support jinja Wait for the unofficial template engine .
Bring their own ORM send Django High coupling with relational database , If you want to use a non relational database , Third party libraries are needed
Self contained database management app
mature , Stable , High development efficiency , be relative to Flask,Django The overall closeness of , It is suitable for the development of enterprise websites .python web The forerunner of the framework , The third party library is rich
2、 Flask It's a lightweight framework , free , flexible , High scalability , Core based Werkzeug WSGI Tools and jinja2 template engine
Suitable for small websites and web Service API, There is no pressure to develop large websites , But the architecture needs to be designed
The combination with relational database is not weaker than Django, And the combination with non relational database is far better than Django
3、 Tornado It's a small and precise way to go , Superior performance , Its most famous asynchronous non blocking design
Tornado Two core modules of :
iostraem: For non blocking socket Simple encapsulation
ioloop: Yes I/O Multiplex packaging , It implements a singleton