Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialMark Büsching
10,117 PointsShowing a button if there is an Item with a specific attribute in jinja.
Hey guys. i've made a small todo-app in flask, that based on the flask community tutorials by Kenneth love. the jinja template gets passed an items variable, that's basically a models.Item.select()
. a single item has the attribute is_done, which is true or false.
on the template, the items are visually divided by the attribute is_done. like this:
<div class="column">
<div class="columns">
<table class="table is-fullwidth">
<tbody>
{% for item in items if not item.is_done %}
<tr>
<td>
<a href="{{ url_for('isdone', item=item.id) }}">{{ item.name }}</a>
</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</div>
<div class="columns">
<div class="column">
<a href="{{ url_for('cleardone') }}" class="button is-light pull-right">Leeren</a>
<table class="table is-fullwidth">
<tbody>
{% for item in items if item.is_done %}
<tr>
<td>
<a href="{{ url_for('isundone', item=item.id) }}" class="has-text-grey-lighter"><s>{{ item.name }}</s></a>
</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</div>
everything works fine. only one thing is bothering me.
the <a href="{{ url_for('cleardone') }}" class="button is-light pull-right">Leeren</a>
button should only appear, if there are actually any items with the attribute is_done=True
.
my question is: is there a clever way in jinja to check, if there is any user in 'users' with the attribute is_done = True? or is it better to do this in flask itself and pass another value to the template?
regards