包含的Django模板中的错误未呈现 [英] Error in included django template is not rendered

查看:87
本文介绍了包含的Django模板中的错误未呈现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用 imgix插件的简单模板,如下所示:

// template_b.html
{% get_imgix project.picture %} 

此行导致错误,应替换为

{% get_imgix project.picture.url %}

这段代码在包含的模板内:

// template_a.html
{% include "template_b.html" %}

似乎当模板标签失败时,在第一种情况下(引发AttributeError),模板被简单地忽略.如果DEBUG设置为False,则不会引发错误.

虽然这对于防止任何面向用户的错误肯定有用,但仍然令人担忧的是,没有任何反馈.

您知道我在哪里可以找到与该行为有关的更多信息,并且是否还有方法可以告知失败?

解决方案

您不是唯一被这种行为吸引住的人.请参阅django-developers邮件列表上的此讨论./p>

在Django 1.11中已弃用了行为.在Django 2.1中,将引发异常.在Django 1.11和2.0中,如果include标签引发了异常,但您收到了弃用警告,但该异常已被静音.

对于早期版本的Django,我没有任何好的建议.如果它对您确实很重要,则可以创建自己的include标签.

I have this simple template that uses the imgix plugin like so:

// template_b.html
{% get_imgix project.picture %} 

This line causes an error and should be replaced by

{% get_imgix project.picture.url %}

This piece of code is inside a template that is included:

// template_a.html
{% include "template_b.html" %}

It seems that when the template tags fails, in the first case (an AttributeError is raised), the template is simply ignored. No error is raised if the DEBUG setting is False.

While this is certainly useful to prevent any user-facing errors, it's still concerning to not have any kind of feedback.

Do you know where I can find more information related to this behavior and if there are ways to still be informed of the failure?

解决方案

You're not the only person to have been caught out from this behaviour. See this discussion on the django-developers mailing list.

The silencing behaviour has been deprecated in Django 1.11. In Django 2.1 the exception will be raised. In Django 1.11 and 2.0 you get a deprecation warning if the include tag raises and exception, but the exception is silenced.

I don't have any good suggestions for earlier versions of Django. You could create your own include tag if it's really important to you.

这篇关于包含的Django模板中的错误未呈现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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