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.
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.
In Django, middleware is a lightweight plugin that processes during request and response execution. Middleware is used to perform a function in the application.
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With