Django:模板如果语句执行,即使是false

时间:2012-08-09 20:22:50

标签: python django

我的模板中有以下内容:

<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

以来,括号不应该存在

1 个答案:

答案 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})