2

I want to use a python list as the options for a check box selection form. So you can select the items in the list that you want and submit the form to use those values.

Which form field do I use in my form?

What would my html file look like?

I think the .html page should look like this.

    <form>
        {% for item in list %}
            <input type="checkbox" name="modList" value={{item}}>{{item}}<br>
        {% endfor %}
    </form>

But I can't figure out what to make the form field in forms.py.

http://packages.python.org/Flask-WTF/

There is no Checkbox form on that page.

I can use

    {{form.modList(formdata = list)}}

But it just displays an emtpy checkbox with no text

1

1 Answer 1

2

I'm not sure why you'd want to do this. If you're using checkboxes, I assume you'll want to return the value of each one. If so, then each one should be a distinct property of the WTForm instance you're using.

If you're only wanting to return the values checked, WTForms offers the wtforms.fields.SelectMultipleField. That would print a dropbox wherein the user could select 0, 1 or more options.

If you must have checkboxes, and you must use a list in the form definition, then it seems your only option is to create a custom field. You can find documentation on how to do that here.

In fact - this use case is the example in the Custom Widget portion of the WTForms documentation.

Sign up to request clarification or add additional context in comments.

3 Comments

The case I have is this: an object has multiple files attached to it. I want a render a form which will let users mark the files they want to delete.
it's been at least two years since I touched WTForms - but if you're using a list in the form definition, as far as I know the only way to do that would be to create a custom widget. It shouldn't be terribly hard to do.
I haven't really groked the process, but I'll try.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.