2012-08-09 54 views
0

我已經在我的模板如下:Django的:模板if語句執行,即使假

<li{% if selected == "al" %} id="selected" {% endif %}><a href="/posting/alerts">Alerts{% if alertnum != 0 %}<span style="color:red">({{alertnum}})</span>{% endif %}</a></li> 

的問題是似乎alertnum != 0。我有以下看法:

def posting_draft(request): 
    user = request.user 
    user_drafts = Draft.objects.filter(user = user) 
    drafts = dict() 
    for d in user_drafts: 
     drafts[d.title] = d.id 
    alertnum = get_alertnum(user) # Returns 0. I have used print statements to verify this 
    return render_to_response('posting_draft.html', {'STATIC_URL':STATIC_URL, 'draft_l' : drafts, 'selected':"dr", alertnum: alertnum}) 

什麼在瀏覽器中顯示,當我打開這個,是Alert()空紅色括號。括號不應該存在,因爲在所有alertnum = 0

回答

4

不知道這是一個錯字,但你忘了周圍的alertnum報價在您的上下文:

return render_to_response('posting_draft.html', 
    {'STATIC_URL':STATIC_URL, 'draft_l' : drafts, 'selected':"dr", alertnum: alertnum}) 

那麼,你是比較0 != 0。它應該是:

return render_to_response('posting_draft.html', 
    {'STATIC_URL':STATIC_URL, 'draft_l' : drafts, 'selected':"dr", 'alertnum': alertnum})