Wednesday, June 25, 2014

testing equality in template tag

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


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

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.


You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to
To post to this group, send email to
Visit this group at
To view this discussion on the web visit
For more options, visit

No comments:

Post a Comment