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:
{{classTimeSlots.{{forloop.counter0}}}}
{{classTimeSlots.[forloop.counter0]}}
{{classTimeSlots.{forloop.counter0}}}
{{classTimeSlots.(forloop.counter0)}}
{{classTimeSlots.forloop.counter0}}
{% 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>
Related
I'm wondering if there is a straightforward way to change the CKAN dataset form. I would be interested in adding extra visibility options than public and private:
that is to say;
Public
Private
Sub Project
Project
Organisation/Company/Institute
CKAN uses package_basic_fields.html
...
{% if show_visibility_selector %}
{% block package_metadata_fields_visibility %}
<div class="form-group control-medium">
<label for="field-private" class="form-label">{{ _('Visibility') }}</label>
<div class="controls">
<select id="field-private" name="private" class="form-control">
{% for option in [('True', _('Private')), ('False', _('Public'))] %}
<option value="{{ option[0] }}" {% if option[0] == data.private|trim %}selected="selected"{% endif %}>{{ option[1] }}</option>
### I want to add extra options here
{% endfor %}
</select>
</div>
</div>
{% endblock %}
{% endif %}
{% if show_organizations_selector and show_visibility_selector %}
...
ckanext-scheming doesn't talk about this either.
Any recommendation for me?
I have looked for other answers but cannot get this one working.
I have model Sitting with a field user_answers which stores a dictionary
{"437": "1574", "319": "hi", "383": "1428", "424": "1528", "203": "785"}
When I do
{% for key, value in sitting %}
<p> {{key}}--{{value}} </p>
<p> {{question.id}}--{{answer.id}} </p>
{% ifequal key question.id %}
{% ifequal value answer.id %}
<li class="list-group-item quiz-answers" >
<span><label for="id_answers_0"><input type="radio" name="answers_{{question.id}}" value="{{answer.id}}" style="margin-right:10px;" id="{{answer.id}}" selected required>
{{answer.content}} </label> </span></li>
{% else %}
<li class="list-group-item quiz-answers" >
<span><label for="id_answers_0"><input type="radio" name="answers_{{question.id}}" value="{{answer.id}}" style="margin-right:10px;" id="{{answer.id}}" required>
{{answer.content}} </label> </span></li>
{% endifequal %}
{% endifequal %}
{% endfor %}
I get the error
Need 2 values to unpack in for loop; got 1.
How can I compare the values so I can select the radio if the key and value matches the question and answer?
Update
{{sitting.items}} in template shows this
{"424": "1529", "437": "1573", "203": "786", "383": "1427", "319": "hi"}
However, when I
{% for key, value in sitting.items %}
<p> {{key}}--{{value}} </p>
<p> {{question.id}}--{{answer.id}} </p>
{% ifequal key question.id %}
{% ifequal value answer.id %}
<li class="list-group-item quiz-answers" >
<span><label for="id_answers_0"><input type="radio" name="answers_{{question.id}}" value="{{answer.id}}" style="margin-right:10px;" id="{{answer.id}}" selected required>
{{answer.content}} </label> </span></li>
{% else %}
<li class="list-group-item quiz-answers" >
<span><label for="id_answers_0"><input type="radio" name="answers_{{question.id}}" value="{{answer.id}}" style="margin-right:10px;" id="{{answer.id}}" required>
{{answer.content}} </label> </span></li>
{% endifequal %}
{% endifequal %}
{% endfor %}
Nothing seems to happpen.
Try:
{% for key, value in sitting.items %}
You need to provide .items when iterating over a dictionary from Python. Good luck!
I am new to Django. I just want to build an online resume based on the details given by the user.
I have separate HTML files for taking the user input and for displaying the resume.
I took the user input for the certification field like this:
<div class="box"><input type="text" name="certificate1" placeholder="Certificate-name"> <input type="text" name="institute1" placeholder="Institute-name"></div><br>
<div class="box"><input type="text" name="certificate2" placeholder="Certicate-name"> <input type="text" name="institute2" placeholder="Institute-name"></div><br>
<div class="box"><input type="text" name="certificate3" placeholder="Certicate-name"> <input type="text" name="institute3" placeholder="Institute-name"></div><br>
<div class="box"><input type="text" name="certificate4" placeholder="Certicate-name"> <input type="text" name="institute4" placeholder="Institute-name"></div><br>
<div class="box"><input type="text" name="certificate5" placeholder="Certificate-name"> <input type="text" name="institute5" placeholder="Institute-name"></div><br>
<div class="box"><input type="text" name="certificate6" placeholder="Certicate-name"> <input type="text" name="institute6" placeholder="Institute-name"></div><br>
And the code which I have written in views.py file is this:
if(request.method=="POST"):
dictionary = {str(i):request.POST[i].capitalize() for i in request.POST}
return render(request,"form/resume.html",dictionary)
And the code which I have written for displaying certifications in the resume is this:
{% if certificate1 %}
<li>{{certificate1}}, {{institute1}}</li>
{% endif %}
{% if certificate2 %}
<li>{{certificate2}}, {{institute2}}</li>
{% endif %}
{% if certificate3 %}
<li>{{certificate3}}, {{institute3}}</li>
{% endif %}
{% if certificate4 %}
<li>{{certificate4}}, {{institute4}}</li>
{% endif %}
{% if certificate5 %}
<li>{{certificate5}}, {{institute5}}</li>
{% endif %}
{% if certificate6 %}
<li>{{certificate6}}, {{institute6}}</li>
{% endif %}
But I feel the code that I have written in displaying certifications in resume [2nd code] is not efficient. Is there any other way of writing the 2nd code? I want to know, how can we use for loop if possible. Or is there any other way? Thanks in advance for your valuable answers.
You can group all your data in one array and pass them in your render function so you could iterate it in your Jinja template
Send certificates as a list to template like this:
certificates = [
{ "certificate": certificate1, "institute": institute1},
{ "certificate": certificate2, "institute": institute2}
]
Then in template, display it like this:
{% for c in certificates %}
<li>{{c.certificate}}, {{c.institute}}</li>
{% endfor %}
Try to use arrays so you can easily iterate through it.
or if you just want to write in that way
use string concatenation for the variable name like:
{% for x in '123456' %}
{% with y=forloop.counter|stringformat:"s" %}
{% with names="certificate"|add:y %}
{% if names %}
<li> {{name}} </li>
{% endif %}
{% endwith %}
{% endwith %}
{% endfor %}
See this for more info:
How can I concatenate forloop.counter to a string in my django template
I have a product list that put 3 products on a row and clears the row and adds another 3, this works fine everywhere but IE6, i know that adding <div> around each group of 3 products will solve this is the template file at the moment
{% for product in category.products.all %}
<div class="{% cycle 'clear' '' '' %}">
<img src="{{MEDIA_URL}}{{product.mini_thumbnail}}" alt="{{product.name}}" class="thumbnail"/>
<div class="prod-details">
<h3>{{product.get_product_name}}</h3>
<h4 class="strap">{{product.get_product_detail}}</h4>
<p>{{ product.strap }}</p>
<ul>
<li class="price">£{{product.price}}</li>
<li class="quantity">
<select name="quantity_{{product.id}}">
<option label="1" value="1">1</option>
<option label="2" value="2">2</option>
<option label="3" value="3">3</option>
<option label="4" value="4">4</option>
<option label="5" value="5">5</option>
<option label="6" value="6">6</option>
<option label="7" value="7">7</option>
<option label="8" value="8">8</option>
<option label="9" value="9">9</option>
</select>
</li>
<li>Details ></li>
<li class="right"><input type="submit" name="add_to_basket_{{product.id}}" value="Add to Basket >"/></li>
</ul>
</div>
</div>
{% endfor %}
codeape's solution only works if you are using a very recent SVN checkout of Django trunk. If you're using version 1.1 or below, that syntax is not supported.
Instead, you can use the divisibleby filter:
{% if forloop.counter|divisibleby:3 %}<div>{% endif %}
Use forloop.counter and a modulo operator inside the loop:
{% for ... %}
{% if forloop.counter|divisibleby:3 %}<div>{% endif %}
...
{% if forloop.counter|divisibleby:3 %}</div>{% endif %}
{% endfor %}
See http://docs.djangoproject.com/en/dev/ref/templates/builtins/#for
EDIT:
Fixed the code example.
I am trying to create a drop down list box with the selected value equal to a value passed from the template values, but with no success. Can anyone take a look and show me what I am doing wrong.
<select name="movie">
{% for movie in movies %}
{% ifequal movie.id selected_movie.id %}
<option value="{{movie.key}}" selected="true">Movie {{movie.id}}: {{movie.name}}</option>
{% endifequal %}
{% ifnotequal movie.id selected_movie.id %}
<option value="{{movie.key}}">Movie {{movie.id}}: {{movie.name}}</option>
{% endifnotequal %}
{% endfor %}
</select>
In this example, movies and selected_movie are passed from the template values.
Please advice!
Your code works for me with django 1.0.2 and firefox 3.5.
You can use {% else %} instead of {% ifnotequal %} and set selected="selected". Hope it helps.
<select name="movie">
{% for movie in movies %}
{% ifequal movie.id selected_movie.id %}
<option value="{{movie.key}}" selected="selected">Movie {{movie.id}}: {{movie.name}}</option>
{% else %}
<option value="{{movie.key}}">Movie {{movie.id}}: {{movie.name}}</option>
{% endifequal %}
{% endfor %}
</select>