3

I have a website with Jinja2 on Google App Engine, so the version is 2.6. At some point, I loop through a list to produce radio buttons and I would like to have the first one checked by default. My code is the following:

     {% for publisher in publishers %}
        <tr onclick="doNav('/spt/publisher/{{ publisher.id }}');" style="cursor: pointer;">
            <td>{{ publisher.name }}</td>
            <td>{{ publisher.songs }}</td>
            <td><input form="export_publisher_form" onclick="event.cancelBubble = true;"
                       type="radio" name="export_publisher" value="{{ publisher.id }}"{% if loop.first %} checked{% endif %}></td>
        </tr>
    {% endfor %}

Problem is, Jinja doesn't seem to return any value for loop.first, nor any loop variable (I tried with loop.index, loop.length and loop.cycle). Am I doing something wrong ?

Edit: publishers is a list that looks like this (indented for clarity):

[{'id': 4974053165105152L, 'name': u'BMG', 'songs': 1}, 
 {'id': 5888297083600896L, 'name': u'Emi', 'songs': 2}, 
 {'id': 6099953071947776L, 'name': u'Ninja Tune', 'songs': 1}, 
 {'id': 4762397176758272L, 'name': u'Sony', 'songs': 0}, 
 {'id': 5325347130179584L, 'name': u'Universal', 'songs': 0}, 
 {'id': 4815173734891520L, 'name': u'Warner', 'songs': 0}]
2
  • 1
    hmmm... don't see anything wrong with the jinja2 code. Can you show the values of publishers? Commented Dec 17, 2013 at 21:09
  • 1
    Thanks for replying Andrew. I don't know if you get notified that I edited the question, so I'm replying too. :) Commented Dec 23, 2013 at 19:13

1 Answer 1

1

Weird... what version of python are you using? When I execute this code I get the following output:

 {% for publisher in heater %}
    <tr onclick="doNav('/spt/publisher/{{ publisher.id }}');" style="cursor: pointer;">
        <td>{{ publisher.name }}</td>
        <td>{{ publisher.songs }}</td>
        <td><input form="export_publisher_form" onclick="event.cancelBubble = true;"
                   type="radio" name="export_publisher" value="{{ publisher.id }}"{% if loop.index == 2 %} checked{% endif %}></td>
    </tr>
{% endfor %}

I get Emi 2 checked. What are you seeing?

I also changed your data to this:

    data = [{'id': 4974053165105152, 'name': 'BMG', 'songs': 1}, 
              {'id': 5888297083600896, 'name': 'Emi', 'songs': 2}, 
              {'id': 6099953071947776, 'name': 'Ninja Tune', 'songs': 1}, 
              {'id': 4762397176758272, 'name': 'Sony', 'songs': 0}, 
              {'id': 5325347130179584, 'name': 'Universal', 'songs': 0}, 
              {'id': 4815173734891520, 'name': 'Warner', 'songs': 0}]
Sign up to request clarification or add additional context in comments.

4 Comments

Ah - I see you said python 2.6. I retried the code with that version of python and got the same successful results. FYI: I'm testing with cherrypy 3.2.4.
I'm using Python 2.7. 2.6 is the version of Jinja2 that is supported by GAE.
It's weird that you're getting Emi checked. It should be the first element. I'll setup a new GAE account to test tomorrow, the problem must be linked to my app, but I have absolutely no idea how.
I used {% if loop.index == 2 %} checked{% endif %} just to check the index was working. You can change that back to loop.first.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.