Re: testing equality in template tag

4 views
Skip to first unread message

Tom Evans

unread,
Jun 26, 2014, 4:49:59 AM6/26/14
to django...@googlegroups.com
On Wed, Jun 25, 2014 at 7:34 PM, Lee Hinde <leeh...@gmail.com> wrote:
> with view code like so:
>
> import calendar
> months_choices = []
> for i in range(1,13):
> months_choices.append((i, calendar.month_name[i]))
> context['months'] = months_choices
>
>
> and
>
> context['default_month'] = today.month
>
> I have this snippet in a template (my first use of lists like this):
>
> <select name="month">
> {% for month in months %}
> <option {% if default_month == month.0 %} selected {% endif %}
> value="{{ month.0 }}">
> {{ month.1 }}</option>
> {% endfor %}
> </select>
>
>
> The issue is the {% if default_month == month.0 %} always returns false...
> The rest of the select is properly built - I get a number for the value and
> the month name as the label.
>
> Why?
>

Differing types? "1" != 1

Log the repr of both values and see.

Cheers

Tom

PS: Django forms are very good at rendering selects and determining
which one is selected.
Reply all
Reply to author
Forward
0 new messages