2016-12-15 17 views
0

我送一个语境词典到HTML页面HTML模板空,但我不知道如何在字典中的一个特定值的if语句执行。这就是我正在做的。检查字典值是在Django/Python的符号

在视图,其中我打电话的视图方法,我返回:

return render(request, 'shop/register.html', {'errors': errors, 'data': data}) 

其中errorsdata也是字典本身。

所以在HTML模板我如何可以访问一个值在errors领域?

我想这样做,但它不工作:

{% if errors.username_error %} //should check if that value is not blank 
    <h3>{{errors.username_error}}</h3> //display error message 
{% endif %} 
+0

如果您只是执行“{{data}}”或“{{errors}}”,那么页面上显示的内容是什么? –

+0

@AndreyShipilov如果我做'{{errors.username_error}}'它显示,但它在if语句中使用'{%%}'notation工作 –

+0

这不是我问的:) –

回答

0

所以,我知道要做到这一点的最好办法是将它作为一个for … empty语句的一部分,就像这样:

{% for error in errors %} 
    <h3>{{error.username_error}}</h3> {# display error message #} 
{% empty %} {# Triggered when the value in the dictionary is blank #} 
    <h3>There is no error message to display</h3> 
{% endfor %}