2016-12-15 18 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 %}