Question

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

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top