私は jinja2 テンプレート システムを django に使用しています。それは本当に速く、私はそれがとても好きです。それにもかかわらず、テンプレートをデバッグする際に問題があります。テンプレートにいくつかのエラー (不適切なタグ、不適切なフィルター名、不適切なブロックの終わり...) を作成した場合、このエラーに関する情報がまったくありません。
たとえば、django ビューでは、次のように記述します。
from jinja2 import Environment, PackageLoader
env = Environment(loader=PackageLoader('main', 'templates'))
def jinja(req):
template = env.get_template('jinja.html')
output=template.render(myvar='hello')
return HttpResponse(output)
私は jinja2 テンプレートを書きます: jinja.html:
{{myvar|notexistingfilter()}} Jinja !
エラーを生成するために意図的に存在しないフィルターを配置したことに注意してください。
「notexistingfilter() が定義されていません」のようなものを期待していましたが、単純な白地に黒のトレースバックしか得られませんでした (通常の django デバッグ メッセージではありません)。
Traceback (most recent call last):
File "/usr/local/lib/python2.6/dist-packages/django/core/servers/basehttp.py", line 279, in run
self.result = application(self.environ, self.start_response)
File "/usr/local/lib/python2.6/dist-packages/django/core/servers/basehttp.py", line 651, in __call__
return self.application(environ, start_response)
File "/usr/local/lib/python2.6/dist-packages/django/core/handlers/wsgi.py", line 241, in __call__
response = self.get_response(request)
File "/usr/local/lib/python2.6/dist-packages/django/core/handlers/base.py", line 134, in get_response
return self.handle_uncaught_exception(request, resolver, exc_info)
File "/usr/local/lib/python2.6/dist-packages/django/core/handlers/base.py", line 154, in handle_uncaught_exception
return debug.technical_500_response(request, *exc_info)
File "/usr/local/lib/python2.6/dist-packages/django/views/debug.py", line 40, in technical_500_response
html = reporter.get_traceback_html()
File "/usr/local/lib/python2.6/dist-packages/django/views/debug.py", line 84, in get_traceback_html
self.get_template_exception_info()
File "/usr/local/lib/python2.6/dist-packages/django/views/debug.py", line 117, in get_template_exception_info
origin, (start, end) = self.exc_value.source
TypeError: 'Template' object is not iterable
エラーが発生したテンプレートのファイル名やエラー自体に関する情報が得られないため、jinja2 のデバッグは非常に困難です。
より多くのデバッグ情報を取得し、jinja2 テンプレート内のエラーの場所を見つけるにはどうすればよいですか?
前もって感謝します、