I have a for loop in a Django template, which prints out a list of books, which works well. The issue comes when I try to mark one of the items in the list as selected, based on a value passed in from the views.py file:
<select name="b">
<option value="1">Book</option>
{% for book in books %}
<option {% if book.id == selected_book %} selected {% endif %} value="{{ book.id }}">{{ book.t }}</option>
{% endfor %}
</select>
The "books" variable is a list, passed in from the views.py file. I can access these variables fine outside of the loop, but if I try to include a seperate variable inside the loop ("selected_book"), I have issues.
The above code does nothing, and if I try to wrap the variable in double brackets
{{ selected_book }}
I get the following error:
TemplateSyntaxError
Could not parse the remainder: '{{' from '{{'
The variable is being passed into the template, because I can get it to print to the page. I only get an error when I try to use it in the for loop.
If I write "{% if book.id == 2 %}" that works fine.
make sure that selected_book is an integer not a string, otherwise the code is fine
Related
My template gets a queryset named qs.value sent from the views.py, I can display the value using {{qs.value}} anywhere in the template, but using it in an if statement raises an error.
{% if {{qs.value}} > 0 %}
<!--do something-->
<h3 class="text-success">{{qs.value}}</h3>
{% else %}
<!--do something else-->
<button class="btn btn-primary">Else</button>
{% endif %}
The error:
Could not parse the remainder: '{{qs.value}}' from '{{qs.value}}'
What am I doing wrong?
{{ }} are used to get the string representation of the variable/fuction. {% %} are used to make some code working. Both can read variables/functions as they are passed with context without any additional {{/{% inside them because they process given arguments directly.
This error usually means you've forgotten a closing quote somewhere in the template you're trying to render. For example: {% url 'my_view %} (wrong) instead of {% url 'my_view' %} (correct). In this case it's the colon that's causing the problem. Normally you'd edit the template to use the correct {% url %} syntax.
I have a python list of files, results, that was created in my flask app.
I create a dropdown select list in my HTML templates by looping through this list. Each file in the list becomes an option with the value and text being the file name. However, I use an if statement to find the query_results.csv file and change the option text to "All Results" instead of using the file name.
<select name="result" method="GET" action="/">
{% for result in results %}
{% if result == 'query_results.csv' %}
<option value="{{ result }}">All Results</option>
{% else %}
<option value="{{ result }}">{{ result }}</option>
{% endif %}
{% endfor %}
</select>
How can I order this select menu alphabetically after I've done this, so that All Results appears at the top where expected? Should I have replaced the item in the list in python and sorted first before building the select menu?
I am passing a string value to select tag in html form and its working properly. But when I load the page again or run the script again, the previously passed values are not shown in drop down list.
In html form, I am passing value like this:
<option value="{{x}}">{{x}}</option>
In python:
x = "Example"
return render_template('example.html', x=x)
can anyone please let me know how to save this value so that it is available for selection in drop down list.
Thanks in advance!
You could set a list, append new option values in it and then pass the list to your html template. Something like this;
# app.py
optionsList = []
optionsList.append('Example')
optionsList.append('Example2')
return render_template('example.html', options=optionsList)
# example.html
{% if options %}
<select name="foo" id="foo">
{% for option in options %}
<option value="{{ option }}">{{ option }}</option>
{% endfor %}
</select>
{% endif %}
I have a queryset generated in my forms.py file that passes into my template. The template result is a multichoice field based on the queryset. The web browser presentation is correct - it renders the queryset as a drop down choice list that I can make a selection from.
Here is the template code:
<tr><td>{{ form.jury_name | placeholder:'Jury Name' }}</td></tr>
<tr><td><select>
{% for item in form.parent_jury.field.queryset %}
<option name="parent_jury" value="{{ item }}">{{ item }}</option>
{% endfor %}
</select></td></tr>
This is all contained in a table.
When the form is submitted (method = "POST") the POST dictionary has all the correct values for the keys except the parent_jury key which posts a value of ''.
I've worked through several SO solutions on the views.py side, but they don't change the fact that the information available for a clean() is missing the choice field value for 'parent_jury'. How do I get the selected option from the list to attach to the 'parent_jury' key?
I think your rendered HTML is not the way it is supposed to be: the name="..." should be part of the <select> tag, not the <option>s:
<tr><td>{{ form.jury_name | placeholder:'Jury Name' }}</td></tr>
<tr><td><select name="parent_jury">
{% for item in form.parent_jury.field.queryset %}
<!-- remove the name here -->
<option value="{{ item }}">{{ item }}</option>
{% endfor %}
</select></td></tr>
(of course you can remove the <!-- comment --> part (this is only meant to draw attention to this change).
I have created a python (2.7) dictionary with the following structure:
cntr_rgns = {'country_1':[region1, region2, region3], 'country_2':[region1, region2] ..}
I pass it to my contextdioctionary as:
ctx['regions'] = cntr_rgns
What I want is to display the values in my template.
In my template view I did that:
{% if regions %}
{% for region in regions.items %}
<option value={{ region }}>{{ region }}</option>
{% endfor %}
{% endif %}
But I again get the unicode values:
(u'Canada',[u'somethong',u'something else',u..])
How can I get back an iteratable JSON or something?
If you want JSON, you need to actually create some JSON.
ctx['regions'] = json.dumps(cntr_rgns)