Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

Introduction — Here’s a quick example:

For example, when you log in to a website, it will show you whether you are logged in when you go to the main page of the website, and it will also show you whether you are logged in when you visit other pages of the website. Do you need to make a login decision in every view of the project?! This is not very troublesome, this time can use middleware is very simple implementation, we just need to customize a middleware, rewrite the method process_request(self,request) :, in this method to log in or not to judge can! Because this method is called before the view is executed, and is called on every request from the user!

This is the genius of middleware!!

The middleware

Now you might be a little confused, but when you finish reading this article, it will be awesome!

Here’s how it works: The user sends a request to the site, which goes through middleware, then to urls, and finally to the view layer. The view layer returns a response to the user, first through the middleware and finally to the user.

  • Middleware in Django. In Django, middleware is just a class that executes middleware methods at the appropriate time when a request comes and goes, according to Django’s own rules.
  • In the Settings file of a Django project, there is a MIDDLEWARE_CLASSES variable, each of which is a middleware element.

(1) Execution order of middleware:

** Requests pass through all layers in top-down order, and responses pass through all layers in bottom-up order after being processed by the View function, with each middleware passing through processing the request or response. **

(2) Five methods can be defined in the middleware, which are:

  1. process_request(self,request) :

Process_view (self, request, callback, Callback_args, callback_kwargs) is called before executing the view, called on each request, and returns None or HttpResponse objects: Process_template_response (self,request,response): process_template_response(self,request,response): Process_exception (self, request, exception) called when the view throws an exception, called on each request, Return an HttpResponse object 5. process_response(self, request, response) All responses are called before returning to the browser, which is called on each request and returns an HttpResponse object

(3) Practical operation steps of customized middleware:

Note: Custom middleware can also be placed anywhere in the project that can be imported (but traditionally in the app root directory)

Steps:

  1. A file mymiddleware.py is created in the home directory;
  2. Create a MyException class in the myMiddleware.py file;
  3. When customizing, you can define the corresponding method for which you want to add functionality, not all of them.

  1. Register middleware:

Register the custom middleware class MyException with settings.py middleware:

Mucis is the project name, first find the file location, add the custom middleware class to the list of MIDLEWARE!

🔆 In The End!

Start now, stick to it, a little progress a day, in the near future, you will thank you for your efforts!

This blogger will continue to update the basic column of crawler and crawler combat column, carefully read this article friends, you can like the collection and comment on your feelings after reading. And can follow this blogger, read more crawler in the days ahead!

If there are mistakes or inappropriate words can be pointed out in the comment area, thank you! If reprint this article please contact me for my consent, and mark the source and the name of the blogger, thank you!