Difference between django middleware between process_request and process_view

I am a little confused about process_request and process_view .

A process request is what you want to pass on request with a request. An example can be taken from request.user .

Then what does process_view do? Is it to execute any kind initially to call any url? As originally, I want to show a home view, but this can be done from the URL.

Can someone give me an example when to use process_view ?

thanks

+7
django middleware
source share
2 answers

process_request is called before Django determines which view should process the request (therefore, this is only the request parameter).

process_view is called after Django determines which view will process the request, but before calling this view. It will have access to the request object along with the view that will handle it and the parameters that will be passed to this view.

Whenever you need to know the view that will be used for the request, you can use process_view . A good example for this is the Django CSRF Middleware process_view , which will not provide CSRF protection if the csrf_exempt decoder is present on the view that the request is intended for:

 def process_view(self, request, callback, callback_args, callback_kwargs): [...] if getattr(callback, 'csrf_exempt', False): return None [...] 
+9
source share

Adrian Giuta wrote a very good answer. I just want to add a few points to this.

process_request is called before the url mapping is done, and process_view is called after the url mapping, but before calling this view.

We can use process_request to change the URL itself and thus invoke a different view. This moment helped me figure it out, so I decided to answer it, maybe it would also be someone else.

0
source share

All Articles