相关疑难解决方法(0)

如何调试Jinja2模板?

我正在使用jinja2模板系统进入django.这真的很快,我很喜欢它.不过,我调试模板时遇到了一些问题:如果我在模板中犯了一些错误(错误的标签,坏的filtername,坏的结尾......),我根本没有关于这个错误的信息.

例如,在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)
Run Code Online (Sandbox Code Playgroud)

我写了一个jinja2模板:jinja.html:

{{myvar|notexistingfilter()}} Jinja !
Run Code Online (Sandbox Code Playgroud)

注意,我故意生成一个不存在的过滤器来生成错误:

我期待像"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 …
Run Code Online (Sandbox Code Playgroud)

python django jinja2

26
推荐指数
2
解决办法
2万
查看次数

标签 统计

django ×1

jinja2 ×1

python ×1