gpt4 book ai didi

python - Django 模板标签中的详细模式

转载 作者:太空狗 更新时间:2023-10-30 00:15:01 25 4
gpt4 key购买 nike

是否有可能在 Debug模式下将有关模板生成的详细信息写入生成的 View ?例如它可以生成这样的输出:

基础.html:

<html>
<body>
{% block content %}
{% endblock %}
</body>
</html>

页面.html:

{% extend "base.html" %}
{% block content %}
Foo
{% include "inner.html" %}
Bar
{% endblock %}

变成这样的形式:

<html>
<body>
<!-- block content -->
<!-- from "page.html" -->
Foo
<!-- include "inner.html" -->
Bar
<!-- endblock content -->
</body>
</html>

为什么?因为有时仅仅通过 IDE 就很难探索一些更大的依赖项。或者,也许您知道一些便于导航(生成图表等)的好工具?当然,这些信息应该只在 Debug模式下生成。在生产中它们应该消失。

最佳答案

您可以使用中间件实现此目的。我在跟踪模板和调用它们的 View 时遇到了类似的问题,所以我编写了一个中间件片段,在 html 响应的顶部添加了一个注释 block 。它并不能完全满足您的要求,但您可以对其进行调整。

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

关于python - Django 模板标签中的详细模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15774132/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com