0

I have the following code in my template (please note the if statement):

{% for base in bases %}
    <label class="checkbox">
        <input name="base" value={{ base.id }} type="checkbox" 
           {% if base.id in selected_bases %}checked="checked" {% endif %}/>
        <span>{{ base.name }}</span>
    </label>
{% endfor %}

The selected_bases variable is a list of unicoded strings: [u'3', u'1', u'5']. base.id is an integer.

How can I make them the same type so that if statement does what I need it to?

Abid A
  • 7,588
  • 4
  • 32
  • 32
  • Can you pass `selected_bases` from the view as integers? – aganders3 Jan 03 '12 at 17:50
  • @aganders3: I'd rather not. `selected_bases` is the list of bases that get passed back and forth `selected_bases = request.GET.getlist('base')`. @gruszczy's solution below does the trick – Abid A Jan 03 '12 at 18:40

2 Answers2

1

I don't know if this work, but try this:

{% if value|stringformat:"d" in selected_bases %}
gruszczy
  • 40,948
  • 31
  • 128
  • 181
0

You should probably do this in the view instead, but you can pipe the list values through the add filter, which does type coercion - or pipe the ints to slugify, which will do the reverse. More information here.

Community
  • 1
  • 1
Eduardo Ivanec
  • 11,668
  • 2
  • 39
  • 42