What is Django's TEMPLATE_DEBUG setting for?

时间秒杀一切 提交于 2019-12-20 12:04:08

问题


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.

(I use django 1.6)


回答1:


This setting helps with debugging errors/exceptions raised while rendering the templates.

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.

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.

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

Documentation reference.


Example.

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 is set to True.

In case of TEMPLATE_DEBUG=True the Django's fancy error page would contain the following block:

If TEMPLATE_DEBUG=False, this block would not be visible.

Hope that helps.



来源:https://stackoverflow.com/questions/25275252/what-is-djangos-template-debug-setting-for

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!