Home > Net >  django class view access response
django class view access response

Time:10-24

in function view:

def view(request):
    # do something with request
    response = render(request, 'view.html')
    # do something with response
    return response

but now I have a View class:

class ArticleDeleteView(View):
    pass


# or even something more complicated
class ArticleDeleteView(LoginRequiredMixin, UserPassesTestMixin, DeleteView):
    pass

which function shall I overwrite to access to the response?


sorry that it may seems a silly question to you, but I looked through the docs and failed to find an answer.


thanks for all suggestions in comment. here's why I need to access to the response. Conditional Django Middleware (or how to exclude the Admin System)

I am building a specific type of 'middleware', for certain class based view, they are not actually middleware since they are not called for each request. I need to access to the response in order to do something before and after the response then return it, therefore I much would like to know which function generate response.

CodePudding user response:

The easiest answer is: you don't. The View classes tend to be uncomplicated enough to be easily rewritten as a custom view which inherently gives you access to the response.

However if you insist I guess one of the functions you could override is the following: as_view, dispatch or setup as the request function goes through all of them. Sadly I couldn't find any mention of the one that was intended for that purpose.

  • Related