使 Django 模板严格

编程入门 行业动态 更新时间:2024-10-24 18:16:06
本文介绍了使 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/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:关键词]

本文发布于:2023-03-27 10:27:13,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/686723.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:模板   Django

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!