sinθ
sinθ

Reputation: 11503

Django: Template if statement executing even when false

I have the following in my template:

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

The problem is with the alertnum != 0 it seems. I have the following view:

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

What shows up in browser when I load this, is Alert () with empty red parenthesis. The parenthesis shouldn't be there at all since alertnum = 0

Upvotes: 0

Views: 207

Answers (1)

Dirk Eschler
Dirk Eschler

Reputation: 2569

Not sure if that's a typo, but you forgot the quotes around the alertnum in your context:

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

So you are comparing 0 != 0. It should be:

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

Upvotes: 4

Related Questions