I have simple middleware which checks if the HTML in the response is valid or not.
我有简单的中间件,它检查响应中的HTML是否有效。
If the HTML is not valid an html is not valid" exception gets raised in development systems.
如果HTML无效,则html无效“在开发系统中引发异常。
Up to now the xception contains the URL and the validation error.
到目前为止,xception包含URL和验证错误。
Then the developer sees the URL in the well known yellow and gray django debug page.
然后开发人员在众所周知的黄色和灰色django调试页面中看到URL。
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.
也许我是盲人,但是如果我看看django调试页面,我看不出哪个方法/视图用破坏的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.
有没有办法在“html无效”例外添加更多信息,以协助开发人员?开发人员应该更容易找到相关的方法/视图。
2 个解决方案
#1
3
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.
process_view钩子允许您访问视图函数,参数和kwargs。您可以将这些存储在请求中,然后在引发“html无效”异常时使用它们。
#2
1
You could use process_view
as Alasdair mentioned, initialize a dictionary for your debug messages and display them with the information needed.
您可以使用process_view作为Alasdair提到的,初始化调试消息的字典并使用所需信息显示它们。
You could also group the dictionary (self.debug_helper['process_request'] = {}')
like below to specify detailed information of the Request/Response.
您还可以将字典(self.debug_helper ['process_request'] = {}')分组,如下所示,以指定请求/响应的详细信息。
__module__
will give you the module in which the view function/class was defined.
__module__将为您提供定义视图函数/类的模块。
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
#1
3
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.
process_view钩子允许您访问视图函数,参数和kwargs。您可以将这些存储在请求中,然后在引发“html无效”异常时使用它们。
#2
1
You could use process_view
as Alasdair mentioned, initialize a dictionary for your debug messages and display them with the information needed.
您可以使用process_view作为Alasdair提到的,初始化调试消息的字典并使用所需信息显示它们。
You could also group the dictionary (self.debug_helper['process_request'] = {}')
like below to specify detailed information of the Request/Response.
您还可以将字典(self.debug_helper ['process_request'] = {}')分组,如下所示,以指定请求/响应的详细信息。
__module__
will give you the module in which the view function/class was defined.
__module__将为您提供定义视图函数/类的模块。
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