Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django Response Middleware: Get name of view which created the response

I have simple middleware which checks if the HTML in the response is valid or not.

If the HTML is not valid an html is not valid" exception gets raised in development systems.

Up to now the xception contains the URL and the validation error.

Then the developer sees the URL in the well known yellow and gray django debug page.

Maybe I am blind, but if I look at the django debug page, I can't see which of my methods/views created the content with the broken html.

Is there a way to add more information to the "html is not valid" exception, to assist the developer? The developer should find the relevant method/view easier.

like image 657
guettli Avatar asked Jan 15 '18 09:01

guettli


People also ask

What is Get_response in Django middleware?

get_response(request) # Code to be executed for each request/response after # the view is called. return response. The get_response callable provided by Django might be the actual view (if this is the last listed middleware) or it might be the next middleware in the chain.

Which of the following middleware are executed during the response phase?

In Django, middleware is a lightweight plugin that processes during request and response execution. Middleware is used to perform a function in the application.

What is HttpResponse in Django?

HttpResponse (source code) provides an inbound HTTP request to a Django web application with a text response. This class is most frequently used as a return object from a Django view.

What kind of data is passed into a view in the request object?

The request object has view input field values in name/value pairs. When we create a submit button then the request type POST is created and calls the POST method. We have four data, those are in Name-Value pairs.


2 Answers

The process_view hook gives you access to the view function, args, and kwargs. You could store these on the request, and then use them when you raise your "html is not valid" exception.

like image 59
Alasdair Avatar answered Oct 04 '22 16:10

Alasdair


You could use process_view as Alasdair mentioned, initialize a dictionary for your debug messages and display them with the information needed.

You could also group the dictionary (self.debug_helper['process_request'] = {}') like below to specify detailed information of the Request/Response.

__module__ will give you the module in which the view function/class was defined.

class CheckForBrokenHtmlMiddleware(MiddlewareMixin):

    def __init__(self, get_response):
        self.get_response = get_response
        self.debug_helper = {}

    def process_request(self, request):
        self.debug_helper = {}
        self.debug_helper['process_request'] = {}
        self.debug_helper['process_request']['path'] = request.path

    def process_view(self, request, view_func, view_args, view_kwargs):
        self.debug_helper['name'] = view_func.__name__
        self.debug_helper['module'] = view_func.__module__
        self.debug_helper['message'] = '"{0}" view caused an error in module "{1}"'.format(
            view_func.__name__, view_func.__module__
        )

    def process_response(self, request, response):            
        print(self.debug_helper)
        return response
like image 43
jazz Avatar answered Oct 04 '22 16:10

jazz