Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python

Mark Büsching
Mark Büsching
10,117 Points

Showing 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