什么是Django的TEMPLATE_DEBUG设置? [英] What is Django's TEMPLATE_DEBUG setting for?
问题描述
我一直在尝试查找有关此设置的信息,但没有太多。有人可以解释一下这个设置是什么?我应该把它关闭在生产中?只是想了解它,也许我在django中缺少一些重要的东西。
I have been trying to find information about this setting but there isn't much. Can someone explain me what is this setting about? Should I turn it off in production?... Just want to learn about it, maybe I'm missing something important in django.
(我使用django 1.6) / p>
(I use django 1.6)
推荐答案
此设置有助于在呈现模板时调试错误/异常。
This setting helps with debugging errors/exceptions raised while rendering the templates.
如果设置为 True
和 DEBUG
是 True
,Django会向您显示通常的花哨错误页面,其中包含追溯,请求详细信息和其他重要信息,突出显示错误发生在哪一行。
If it is set to True
and DEBUG
is True
, Django would show you usual "fancy" error page with a traceback, request details and other important information, and highlight at which line the error happened.
如果设置为 False
和 DEBUG
是 True
并且渲染模板时出现错误,您仍然会看到Django的错误页面,但是会错过包含发生错误的模板代码的块。所以调试会更加困难。
If it is set to False
and DEBUG
is True
and there was an error while rendering the template, you would still see the Django's error page, but it would miss the block containing template code where the error occurred. So it would be more difficult to debug.
确保 TEMPLATE_DEBUG
的值是一个很好的做法与 DEBUG
相同(虽然如果 DEBUG
是 False
错误页面不会显示):
It is a good practice to make sure the value of TEMPLATE_DEBUG
is the same as DEBUG
(though if DEBUG
is False
, the error page would not be shown):
DEBUG = TEMPLATE_DEBUG = True # development
DEBUG = TEMPLATE_DEBUG = False # production
文档参考。
示例。
想象一下我们在模板中有错误,忘记在现在提供日期格式
模板标签: / p>
Imagine we have an error in the template, forgot to provide the date format in the now
template tag:
<div>
<span class="date">
{% now %}
</span>
</div>
DEBUG
设置为 True
。
如果 TEMPLATE_DEBUG = True
Django的花哨错误页面将包含以下块:
In case of TEMPLATE_DEBUG=True
the Django's fancy error page would contain the following block:
如果 TEMPLATE_DEBUG = False
,此块将不可见。
希望有所帮助。
这篇关于什么是Django的TEMPLATE_DEBUG设置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!