使 Django 模板严格 [英] Make django templates strict

查看:16
本文介绍了使 Django 模板严格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 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.com/group/google-appengine/browse_thread/thread/86a5b12ff868038d,这听起来很骇人听闻.

The only hint at a solution I've found online is http://groups.google.com/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.com/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.com/en/2.0/topics/templates/#module-django.template.backends.django

另请阅读:http://docs.djangoproject.com/en/dev/ref/templates/api/#invalid-template-variables

这篇关于使 Django 模板严格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆