これを実現するには、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
良い質問! ['django-debug-toolbar'](https://github.com/django-debug-toolbar/django-debug-toolbar)または[' django-template-repl'](https:// github .com/codysoyland/django-template-repl)が役に立ちます。 –
私の場合、 'django-template-repl'はファイルのツリー全体を入力する必要があるので、無駄です。どのファイルが悪いのか分かっていれば、私はそれがなくても答えが出ます。私はこのモデルを作成しました。これは、挿入されたモデル(テンプレート{list} .html ")')と出力は、内容を使用している別個のテンプレートにあまりにも似ています。 'django-debug-toolbar'は近いですが、十分ではありません。 – zwierzak
魔法は悪です。すべてのブロック/ includeとbeforの後にhtmlコメントを追加するための小さなsedスクリプトを考えましたか? – ornoone