简体   繁体   中英

django template syntax to iterate through two lists

I have the following django template, where as I'm iterating through a list ( class_list_overall ), I want to use forloop.counter0 as an index in another list ( classTimeSlots ). It just keeps giving me a TemplateSyntaxError . I have tried the following variations:

  1. {{classTimeSlots.{{forloop.counter0}}}}
  2. {{classTimeSlots.[forloop.counter0]}}
  3. {{classTimeSlots.{forloop.counter0}}}
  4. {{classTimeSlots.(forloop.counter0)}}
  5. {{classTimeSlots.forloop.counter0}}
  6. {% with forloop.counter0 as index%} <legend>{{ classTimeSlots.index}}</legend> {% endwith %}

None of which worked. Any suggestions? I'm just a newbie at DJango. I'm using Google App Engine.

Here's the code snippet (I know it's inefficient but I've been trying different things):

{% for class_list in class_list_overall %}
    <fieldset> <legend>{{ classTimeSlots.forloop.counter0 }}</legend>
        <ul>
            <li> <label>First Choice </label>
                <select class="dropdown" name="class{{forloop.counter}}1" size="1">
                    <option value="Click Here to Choose" selected="selected">Click Here to Choose</option>
                    {% for class in class_list %}
                        <option>{{class}}</option>
                    {% endfor %}
                </select>
            </li>
            <li> 
                <label>Second Choice </label>
                <select class="dropdown" name="class{{forloop.counter}}2" size="1">
                    <option value="Click Here to Choose" selected="selected">Click Here to Choose</option>
                    {% for class in class_list %}
                        <option>{{class}}</option>
                    {% endfor %}
                </select>
            </li>
        </ul>
    </fieldset>
{% endfor %}

Short answer: you can't do that.

The template language will not try to determine the value of a variable passed in dot syntax.

It will do a literal lookup of forloop.counter0

1: write a template tag that accepts a variable and a key, and have it return the variable[key]

2: this can most likely be done in the view. Can I see it?

Django doesn't support this - it's deliberately limited. Instead, you should modify your view function to zip the two lists together, and pass that in to the template.

Its possible but not recommendable:

{% for class_list in class_list_overall %}
<fieldset> <legend>
   {% for cts in classTimeSlots %}
    {% ifequal forloop.counter forloop.parentloop.counter %} {{cts}} 
    {% endifequal %} 
   {% endfor %} </legend>
    <ul>
        <li> <label>First Choice </label>
            <select class="dropdown" name="class{{forloop.counter}}1" size="1">
                <option value="Click Here to Choose" selected="selected">Click Here to Choose</option>
                {% for class in class_list %}
                    <option>{{class}}</option>
                {% endfor %}
            </select>
        </li>
        <li> 
            <label>Second Choice </label>
            <select class="dropdown" name="class{{forloop.counter}}2" size="1">
                <option value="Click Here to Choose" selected="selected">Click Here to Choose</option>
                {% for class in class_list %}
                    <option>{{class}}</option>
                {% endfor %}
            </select>
        </li>
    </ul>
</fieldset>{% endfor %}

But its better to take list into parent dict:

[class_list_overall[i].update({'label':classTimeSlots[i]}) for i in range(0,len(classTimeSlots))]

And then change above code to:

<legend>
   {{ class_list.label }}
</legend>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM