如果在django模板的渲染中缺少模板,我会得到如下的异常。
经过很长时间的搜索,我找到了伪造的部分:
{% include form.template_name %}
form.template_name在我的上下文中为空。
如何在不搜索数小时的情况下找到相关的模板名称?
我缺少像普通python代码那样的回溯。 "正常" python tracebacks向我显示包含错误的文件和行。
/home/foo_fm_d/bin/python /usr/local/pycharm-community-4.5/helpers/pycharm/utrunner.py /home/foo_fm_d/src/foo-time/foo_time/tests/unit/views/user/test_preview_of_next_days.py::EditTestCase::test_preview_of_next_days true
Testing started at 09:26 ...
Error
Traceback (most recent call last):
File "/home/foo_fm_d/src/foo-time/foo_time/tests/unit/views/user/test_preview_of_next_days.py", line 11, in test_preview_of_next_days
self.admin_client.get(url)
File "/home/foo_fm_d/src/djangotools/djangotools/utils/testutils.py", line 275, in get
response = super(Client, self).get(path, data, **extra)
File "/home/foo_fm_d/local/lib/python2.7/site-packages/django/test/client.py", line 473, in get
response = super(Client, self).get(path, data=data, **extra)
File "/home/foo_fm_d/local/lib/python2.7/site-packages/django/test/client.py", line 280, in get
return self.request(**r)
File "/home/foo_fm_d/local/lib/python2.7/site-packages/django/test/client.py", line 444, in request
six.reraise(*exc_info)
File "/home/foo_fm_d/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 137, in get_response
response = response.render()
File "/home/foo_fm_d/local/lib/python2.7/site-packages/django/template/response.py", line 105, in render
self.content = self.rendered_content
File "/home/foo_fm_d/local/lib/python2.7/site-packages/django/template/response.py", line 82, in rendered_content
content = template.render(context)
File "/home/foo_fm_d/local/lib/python2.7/site-packages/django/template/base.py", line 140, in render
return self._render(context)
File "/home/foo_fm_d/local/lib/python2.7/site-packages/django/template/base.py", line 134, in _render
return self.nodelist.render(context)
File "/home/foo_fm_d/local/lib/python2.7/site-packages/django/template/base.py", line 840, in render
bit = self.render_node(node, context)
File "/home/foo_fm_d/local/lib/python2.7/site-packages/django/template/debug.py", line 78, in render_node
return node.render(context)
File "/home/foo_fm_d/local/lib/python2.7/site-packages/django/template/loader_tags.py", line 123, in render
return compiled_parent._render(context)
File "/home/foo_fm_d/local/lib/python2.7/site-packages/django/template/base.py", line 134, in _render
return self.nodelist.render(context)
File "/home/foo_fm_d/local/lib/python2.7/site-packages/django/template/base.py", line 840, in render
bit = self.render_node(node, context)
File "/home/foo_fm_d/local/lib/python2.7/site-packages/django/template/debug.py", line 78, in render_node
return node.render(context)
File "/home/foo_fm_d/local/lib/python2.7/site-packages/django/template/loader_tags.py", line 123, in render
return compiled_parent._render(context)
File "/home/foo_fm_d/local/lib/python2.7/site-packages/django/template/base.py", line 134, in _render
return self.nodelist.render(context)
File "/home/foo_fm_d/local/lib/python2.7/site-packages/django/template/base.py", line 840, in render
bit = self.render_node(node, context)
File "/home/foo_fm_d/local/lib/python2.7/site-packages/django/template/debug.py", line 78, in render_node
return node.render(context)
File "/home/foo_fm_d/local/lib/python2.7/site-packages/django/template/loader_tags.py", line 62, in render
result = block.nodelist.render(context)
File "/home/foo_fm_d/local/lib/python2.7/site-packages/django/template/base.py", line 840, in render
bit = self.render_node(node, context)
File "/home/foo_fm_d/local/lib/python2.7/site-packages/django/template/debug.py", line 78, in render_node
return node.render(context)
File "/home/foo_fm_d/local/lib/python2.7/site-packages/django/template/debug.py", line 88, in render
output = self.filter_expression.resolve(context)
File "/home/foo_fm_d/local/lib/python2.7/site-packages/django/template/base.py", line 585, in resolve
obj = self.var.resolve(context)
File "/home/foo_fm_d/local/lib/python2.7/site-packages/django/template/base.py", line 735, in resolve
value = self._resolve_lookup(context)
File "/home/foo_fm_d/local/lib/python2.7/site-packages/django/template/base.py", line 789, in _resolve_lookup
current = current()
File "/home/foo_fm_d/local/lib/python2.7/site-packages/django/template/loader_tags.py", line 72, in super
return mark_safe(self.render(self.context))
File "/home/foo_fm_d/local/lib/python2.7/site-packages/django/template/loader_tags.py", line 62, in render
result = block.nodelist.render(context)
File "/home/foo_fm_d/local/lib/python2.7/site-packages/django/template/base.py", line 840, in render
bit = self.render_node(node, context)
File "/home/foo_fm_d/local/lib/python2.7/site-packages/django/template/debug.py", line 78, in render_node
return node.render(context)
File "/home/foo_fm_d/local/lib/python2.7/site-packages/django/template/loader_tags.py", line 62, in render
result = block.nodelist.render(context)
File "/home/foo_fm_d/local/lib/python2.7/site-packages/django/template/base.py", line 840, in render
bit = self.render_node(node, context)
File "/home/foo_fm_d/local/lib/python2.7/site-packages/django/template/debug.py", line 78, in render_node
return node.render(context)
File "/home/foo_fm_d/local/lib/python2.7/site-packages/django/template/loader_tags.py", line 62, in render
result = block.nodelist.render(context)
File "/home/foo_fm_d/local/lib/python2.7/site-packages/django/template/base.py", line 840, in render
bit = self.render_node(node, context)
File "/home/foo_fm_d/local/lib/python2.7/site-packages/django/template/debug.py", line 78, in render_node
return node.render(context)
File "/home/foo_fm_d/local/lib/python2.7/site-packages/django/template/loader_tags.py", line 166, in render
template = get_template(template_name)
File "/home/foo_fm_d/local/lib/python2.7/site-packages/django/template/loader.py", line 138, in get_template
template, origin = find_template(template_name)
File "/home/foo_fm_d/local/lib/python2.7/site-packages/django/template/loader.py", line 131, in find_template
raise TemplateDoesNotExist(name)
TemplateDoesNotExist
Process finished with exit code 0
更新
我的个人背景:到目前为止,我避免使用django模板,因为有时异常会被忽略,而这样的追溯会让调试过程感觉就像穿着厚重的混凝土鞋一样。
我试图摆脱我的偏见。或者至少找到一种如何从模板中获得更好的错误消息的方法。
更新II
我通过pyCharm中的unittest看到了回溯。我没有阅读视图的结果。 " webbrowser"客户端调用视图。我设置TEMPLATE_DEBUG = True,但结果是一样的。
答案 0 :(得分:3)
你没有看到通常的Python堆栈跟踪的原因恰恰是Django模板不是Python。
它是一种特定语言,它本身由Python解释,它基于模板构建抽象语法树,然后在呈现阶段评估此树。此时,默认情况下无法访问与源相关的信息(例如模板文件)。
Django中有一个选项可以显示与渲染模板时引发的异常相关的更多相关信息,在DJango 1.8之前它是TEMPLATE_DEBUG
。
请参阅https://docs.djangoproject.com/en/1.8/ref/settings/#template-debug
此选项随Django 1.8更改,并且引入多个模板引擎作为调试信息特定于模板引擎的每个实现。
答案 1 :(得分:2)
说实话,我总是使用其他海报建议的实时服务器机制。但是,由于您正在寻找可能在Jenkins中运行的解决方案,而您的堆栈显示您正在通过debug.py,我查看了那里的调试数据。
我注意到模板引擎应该在Exception的django_template_source属性中为模板的失败部分添加一段源代码。
这是否存在于您的例外并且有帮助?如果是这样,您可以捕获异常并在失败UT之前将其打印出来。
答案 2 :(得分:0)
当您似乎使用PyCharm时,您可以使用调试器来查看模板的上下文(为此设置断点)documentation
另外,您使用的是哪个版本的Django?在我的(python3.4上的1.8)中,loader.py文件中没有find_template()。它可能因此被删除了?
顺便说一下,TemplateDoesNotExist似乎总是以模板的名称作为参数调用,我猜想能够在错误消息中显示它,但在你的情况下,它是空的。也许它是难以调试的另一个原因?