Django模板标记中的详细模式

2024-04-24 07:56:12 发布

您现在位置:Python中文网/ 问答频道 /正文

在调试模式下,是否有可能在生成的视图中写入有关模板生成的详细信息?例如,它可以产生这样的输出:

在基本.html公司名称:

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

在页面.html公司名称:

^{pr2}$

以这种形式:

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

为什么?因为有时候仅仅通过IDE就很难探索一些更大的依赖关系。或者你知道一些很好的工具来简化导航(生成图形等)?当然,这些信息只能在调试模式下生成。在生产中,它们应该消失。在


Tags: from名称视图模板html详细信息公司body
1条回答
网友
1楼 · 发布于 2024-04-24 07:56:12

您可以使用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

相关问题 更多 >