Skip to content

Instantly share code, notes, and snippets.

@mercul3s
Last active August 29, 2015 13:57
Show Gist options
  • Save mercul3s/9688302 to your computer and use it in GitHub Desktop.
Save mercul3s/9688302 to your computer and use it in GitHub Desktop.
This is an example of looping through a dictionary in Jinja syntax, storing all the items in the dictionary in fields in a table. The table header rows are created outside of the loop, and the fields are created inside the for loop for each item in the dictionary. You can do the same thing with a list of objects by using syntax such as item.item…
# Here's how the controller route and function look:
@app.route("/search_items", methods=["POST"])
def search_items():
search_terms = form.search.data
results = search(search_terms) # search is a function I've defined in another file and imported
return render_template("search_results.html", search_res=results)
<div id="search_results">
<table>
<tr>
<th>Item</th>
<th>Description</th>
<th>Time Found</th>
<th>Location Found</th>
<th>Image</th>
<th>Select Item</th>
</tr>
{% for item in search_res %}
<tr>
<td>{{ item["item_type"] }}</td>
<td>{{ item["description"] }}</td>
<td>{{ item["time_found"] }}</td>
<td>{{ item["location"] }}</td>
<td>{{ item["image"]}}</td>
<td data-toggle="buttons">
<label>
<input type="checkbox" name="lost_options" id={{ item['id'] }}>Claim
</label>
</td>
</tr>
{% endfor %}
</table>
<div><p>{{ search_res }}</p>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment