Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Iterate through checkboxes in Flask

I have a Jinja2 template that looks like this:

<form action="" method=post>
    <table>
        <tr>
            <th></th>
            <th>ID</th>
            <th>Title</th>
        </tr>
        {% for page in pages %}
            <tr>
                <td><input type=checkbox name=do_delete value="{{ page['id'] }}"></td>
                <td>{{ page['id'] }}</td>
                <td><a href="{{ page['id'] }}">{{ page['title'] }}</a></td>
            </tr>
        {% endfor %}
    </table>
    With selected:
    <input type=submit value=Delete>
</form>

And I have a function, that should delete the pages according to which checkboxes were checked, when the 'Delete' button is clicked:

db.session.query(Page).filter(Page.id.in_(page_ids)).delete()

What I'm stuck with is how do I iterate through all the checkboxes and form the page_ids list of checked ones.

like image 512
Andrii Yurchuk Avatar asked Nov 03 '11 13:11

Andrii Yurchuk


1 Answers

Flask's request object (well, actually the class that is returned by the LocalProxy instance that is request) is a subclass of werkzeug's MultiDict data structure - which includes a getlist method.

page_ids = request.form.getlist("do_delete")
like image 177
Sean Vieira Avatar answered Sep 22 '22 04:09

Sean Vieira