问题描述
在 Django 模板中,如果 var
未定义,对 {{ var }}
的调用将无声地失败.这使得模板难以调试.有没有我可以切换的设置,以便 django 在这种情况下会抛出异常?
In a django template, a call to {{ var }}
will silently fail if var
is undefined. That makes templates hard to debug. Is there a setting I can switch so django will throw an exception in this case?
我在网上找到的解决方案的唯一提示是 http://groups.google/group/google-appengine/browse_thread/thread/86a5b12ff868038d,这听起来很骇人听闻.
The only hint at a solution I've found online is http://groups.google/group/google-appengine/browse_thread/thread/86a5b12ff868038d and that sounds awfully hacky.
推荐答案
Django<=1.9
在您的 settings.py
中设置 TEMPLATE_STRING_IF_INVALID = 'DEBUG WARNING: undefined template variable [%s] not found'
.
查看文档:
https://docs.djangoproject/en/1.9/ref/settings/#template-string-if-invalid
Django>=1.10
在 settings.py
中设置 string_if_invalid = 'DEBUG WARNING: undefined template variable [%s] not found'
模板选项.
Set string_if_invalid = 'DEBUG WARNING: undefined template variable [%s] not found'
template option in your settings.py
.
参见文档:https:///docs.djangoproject/en/2.0/topics/templates/#module-django.template.backends.django
另请阅读:http://docs.djangoproject/en/dev/ref/templates/api/#invalid-template-variables
这篇关于使 Django 模板严格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
更多推荐
[db:关键词]
发布评论