1

我正在尝试调整我为家庭项目编写的中间件。中间件的目标是将注释块附加到 HTML 或 XML(或其他用户定义的模板类型)的顶部,以显示有关该模板的信息,例如调用它的视图、模板位置等。它对于调试非常有用,尤其是在处理您不熟悉的项目时,因为它允许您快速识别模板及其位置。

问题是,为了获得模板名称,它依赖于在视图中使用 SimpleTemplateResponse,因为它包含模板名称,因此可以在 process_template_response() 的中间件中获取它。

这显然使得中间件对于移植到现有项目非常不切实际,因为通常使用 render_to_response() ,并且返回一个 HttpResponse 对象,该对象不再知道使用什么模板来呈现它。

编写模板标识符的第一个实例(我从中修改了我的版本)的人,我在我曾经从事的项目中遇到过,使用 threading.currentThread() 或类似方法并以这种方式找到模板名称并传递它到中间件,但我不再有权访问该代码,所以我无法检查它是如何完成的。

有谁知道访问模板名称的方法不涉及使用 SimpleTemplateResponse,或者理想情况下修补 render_to_response(尽管如果它似乎是唯一的方法,我会接受)?

中间件:

# Base template for the html/url locator comment 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:
    """
    Middleware class that adds a comment block to an html response object.
    Currently adds url, referer, the python module and its function/line,
    the function arguments, and the template passed in. Only triggers if
    a SimpleTemplateResponse object was used to render the template and
    the template is .html.
    """

    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):
        import threading
        print threading.current_thread.request
        if settings.DEBUG:
            if self.valid_template:
                block = self._populate_comment_block()
                response.content = "%s%s" % (block, response.content)
        return response
4

2 回答 2

2

您可能想使用django-debug-toolbar和它的模板面板,而不是重新发明轮子。

仅供参考,如您所见,它通过猴子修补 djangoTemplate类获取模板名称。该代码基于此代码段

希望有帮助。

于 2013-07-05T12:29:40.137 回答
0

如果您的视图是基于类的,您可以执行以下操作:

from contextlib import suppress

class MyMiddleware(SessionMiddleware):
    def process_response(self, request, response):
        html = response.content
        dostuff()
        return super().process_response(request, response)

    def process_view(self, request, view_func, view_args, view_kwargs):
        with suppress(AttributeError):
            template_name = view_func.view_class.template_name
        with suppress(AttributeError):
            # Do something else for function based views, e.g. add it as an attribute to the function
            return view_func.template_name
        print(template_name)
        return None
于 2021-11-10T14:03:44.377 回答