2

Using FlaskForm and SelectMultipleField, I'm creating a table of choices allowing for multiple selection to be ranked: 1, 2, 3, ... .

In order to place the choices in a grid pattern on a table, I made each row it's own instance of SelectMultipleField.

The submit button is only returning the values of the first instance of SelectMultipleField (dogs).

How can I get the submit button to return values in all instances of SelectMultipleField?

Here's the class in my forms module:

class LPRForm(FlaskForm):
    party = ['Dogs', 'Cats', 'Rabbits', 'Birds']
    dog = [('', 'D. Duke'), ('', 'R. Rusty'), \
             ('', 'T. Tucker'), ('', 'R. Roger')]
    cat = [('', 'S. Shadow'), ('', 'M. Misty'), \
             ('', 'P. Patch'), ('', 'P. Paws')]
    rabbit = [('', ''), ('', 'C. Clover'), ('', ''), ('', '')]
    bird = [('', 'P. Pikachu'), ('', 'S. Starburst'), \
              ('', ''), ('', 'F. Flighty')]

    vote_dog = SelectMultipleField('District', choices=dog, 
                        option_widget=widgets.TextInput() )
    vote_cat = SelectMultipleField('District', choices=cat, 
                        option_widget=widgets.TextInput() )
    vote_rabbit = SelectMultipleField('District', choices=rabbit, 
                        option_widget=widgets.TextInput() )
    vote_bird = SelectMultipleField('District', choices=bird, 
                        option_widget=widgets.TextInput() )

    submit = SubmitField('Cast Ballot')

Here's the relevant protion of the html file:

<table style="width:100%" align="center">
    <tr>
    <td>  </td>
    {% for vote in form.vote_dog %}
        {% if vote.label != '': %}
            <td>{{ vote(size="2") }}</td> <td>{{ vote.label }}</td>

        {% endif %}
    {% endfor %}
    </tr>

    <tr>
    <td>  </td>
    {% for vote in form.vote_cat %}
        {% if vote.label != '': %}
            <td>{{ vote(size="2") }}</td> <td>{{ vote.label }}</td>

        {% endif %}
    {% endfor %}
    </tr>

    <tr>
    <td>  </td>
    {% for vote in form.vote_rabbit %}
        {% if vote.label != '': %}
            <td>{{ vote(size="2") }}</td> <td>{{ vote.label }}</td>

        {% endif %}
    {% endfor %}
    </tr>

    <tr>
    <td>  </td>
    {% for vote in form.vote_bird %}
        {% if vote.label != '': %}
            <td>{{ vote(size="2") }}</td> <td>{{ vote.label }}</td>

        {% endif %}
    {% endfor %}
    </tr>
</table>
</td>
</tr>

{{ form.submit }}

And the view module:

@app.route('/lpr', methods=['GET', 'POST'])
def lpr():
    form = LPRForm() 
    return render_template('lpr.html', title='Home', form=form)
3
  • Does your html template have the form tags? Also lpr() will need form = LPRForm(request.POST) or similar, otherwise you are always processing a new form with no user data submitted (the form for GET method). Commented Feb 3, 2018 at 19:16
  • @progmatico My template does not have form tags. I'm using form=LPRForm() and the browser is submitting the user data back to the server. What's the purpose of request.POST? Commented Feb 4, 2018 at 20:26
  • Humm... Your code is ok. I suggested the usual pattern with bottle and WTForms. Looks like Flask has slight differences, the pattern does not apply. request.POST gets you a MultiDict in bottle and then you instantiate the form with it as a template for the form object attributes (after the post action the fields will be filled). I think from the Flask docs there is no request.POST, and looks like it is not needed this way. Probably request.form gives the same thing. Commented Feb 5, 2018 at 20:23

1 Answer 1

1

I was able to get this working by using FormField. However, the documentation gives a default separator as -. Additionally, I explicitly identified separator='-', but only when I used . as a separator, when calling the class, did it work properly, ... even with separator='-'.

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

Comments

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.