miladhzz / django-custom-middleware

A simple project with a custom middleware

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

This repository created in attach to this post

What are middlewares?

In short, middlewares are classes that contain some functions called before and after all view functions. For more detail on middleware, you can click here.

What requirements should middlewares have?

the only requirement for a class to be a middleware is that middleware must have at least one of the following methods:

  • process_request
  • process_view
  • process_response
  • process_exception

How do these methods work?

Every Django project has a list called MIDDLEWARES (or MIDDLEWARE_CLASSES in older versions) in settings.py that contains the path of every middleware that has at least one of the methods mentioned above. after request received by the framework, WSGI Handler builds a HttpRequest object for passing to view functions and in this process it iterates through the list and it will call process_request method from every class in the list that contains this method. (if one class in the list doesn't have process_request , it will just get skipped.) after building HttpRequest object and before passing it to every view function WSGI Handler will call process_view method of middlewares.

  • process_request and process_view methods should return None that in this case WSGI Handler will continue the cycle; or this methods should alternatively return HttpResponse that in this case WSGI Handler will begin process_response cycle. if a view function raise an exception WSGI Handler will begin process_exception cycle and if a view function returns HttpResponse object, WSGI Handler will begin process_response cycle.
  • unlike process_request and process_view methods, process_response and process_exception can only modify the data of response/exception

About

A simple project with a custom middleware


Languages

Language:Python 95.4%Language:JavaScript 1.7%Language:HTML 1.7%Language:CSS 1.2%Language:Shell 0.1%