Is there any possibility to write into generated view verbose informations about template generation in debug mode? For example it could generate such output:
是否有可能在調試模式下寫入有關模板生成的生成視圖詳細信息?例如,它可以生成這樣的輸出:
base.html:
<html>
<body>
{% block content %}
{% endblock %}
</body>
</html>
page.html:
{% extend "base.html" %}
{% block content %}
Foo
{% include "inner.html" %}
Bar
{% endblock %}
Into such form:
進入這樣的形式:
<html>
<body>
<!-- block content -->
<!-- from "page.html" -->
Foo
<!-- include "inner.html" -->
Bar
<!-- endblock content -->
</body>
</html>
Why? Because sometimes exploring some larger dependencies are very hard just by IDE. Or maybe you know some good tool for easier navigation (generating graphs etc.)? Of course this informations should be generated only in debug mode. In production they should disappear.
為什么?因為有時只是通過IDE來探索一些更大的依賴項是非常困難的。或者您可能知道一些更好的工具,以便更輕松地導航(生成圖表等)?當然,只應在調試模式下生成此信息。在生產中,它們應該消失。
2
You might be able to achieve this using middlware. I was having a similar issue a while back keeping track of templates and the views calling them so I wrote a middleware snippet that added a comment block to the top of the html response. It doesn't quite do what you're asking but you might be able to adapt it.
您可以使用middlware實現此目的。我有一個類似的問題,一段時間跟蹤模板和調用它們的視圖,所以我寫了一個中間件片段,在html響應的頂部添加了一個注釋塊。它並沒有完全按照您的要求進行,但您可以對其進行調整。
COMMENT_BLOCK = """
<!--
[ url ] >> http://%(host)s%(path)s
[ referer ] >> %(referer)s
[ module ] >> %(module)s
[ function ] >> %(function)s, line %(line)s
[ args ] >> args=%(args)s, kwargs=%(kwargs)s, defaults=%(defaults)s
[ template ] >> %(template)s
-->
"""
# Add any additional template types you wish to add the comment block to.
MIMETYPES = (
"text/html",
"text/xml",
)
class HtmlTemplateFinder:
def __init__(self):
self.host = None
self.referer = None
self.path = None
self.module = None
self.function = None
self.line = None
self.args = None
self.kwargs = None
self.defaults = None
self.template = None
self.valid_template = False
def _populate_comment_block(self):
return COMMENT_BLOCK % {
'host': self.host,
'referer': self.referer,
'path': self.path,
'module': self.module,
'function': self.function,
'line': self.line,
'args': self.args,
'kwargs': self.kwargs,
'defaults': self.defaults,
'template': self.template,
}
def process_view(self, request, view_func, view_args, view_kwargs):
self.host = request.META.get('HTTP_HOST', None)
self.referer = request.META.get('HTTP_REFERER', None)
self.path = request.path
self.module = view_func.func_code.co_filename
self.function = ('.').join((view_func.__module__, view_func.func_name))
self.line = view_func.func_code.co_firstlineno
self.args = view_args
self.kwargs = view_kwargs
self.defaults = view_func.func_defaults
return None
def process_template_response(self, request, response):
from mimetypes import guess_type
# Use this rather than response.template_name, this always returns str
self.template = response.resolve_template(response.template_name).name
self.valid_template = guess_type(self.template)[0] in MIMETYPES
return response
def process_response(self, request, response):
from <your app> import settings
if settings.DEBUG:
if self.valid_template:
block = self._populate_comment_block()
response.content = "%s%s" % (block, response.content)
return response
本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2013/04/02/72075fe2d1b8b2294a8825fa4f0f7d1b.html。