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


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?

--
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 django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CA%2BePoMzx%2B4774Ne21djP-EdSAHDL_64DWhZVQYrUYdND%3DBk2%2Bw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment