2017-06-02 103 views
0

我想比較一個字典值與常量,我注意到這些正在處理不同,基於我是否使用if/elif語句,如果我只是比較值直接存儲在常量中的值。以下是顯示上下文詞典和模板的相關部分的示例(僅限選項2和3)。我在這裏錯過了爲什麼選項1不起作用?Django elif模板標籤呈現常量

視圖:

STATUS1 = 1 
STATUS2 = 2 
STATUS3 = 3 

context = { 
    'obj1': { 
     'status': STATUS3, 
     'otherattrs': 'other stuff' 
    }, 
    'STATUS1': STATUS1, 
    'STATUS2': STATUS2, 
    'STATUS3': STATUS3 
} 

選項1個模板:

{% if obj1.status == STATUS1 %} 
    button 1 
{% elif obj1.status == STATUS2 %} 
    button 2 
{% elif obj1.status == STATUS3 %} 
    button 3 
{% endif %} 

選項2模板:

{% if obj1.status == STATUS1 %} 
    button 1 
{% endif %} 
{% if obj1.status == STATUS2 %} 
    button 2 
{% endif %} 
{% if obj1.status == STATUS3 %} 
    button 3 
{% endif %} 

選項3的模板:

{% if obj1.status == 1 %} 
    button 1 
{% elif obj1.status == 2 %} 
    button 2 
{% elif obj1.status == 3 %} 
    button 3 
{% endif %} 

回答

0

無法重現您的問題,所有三種方法爲我輸出button 3。選項一你看到了什麼結果?