0

My form.py is like this:

class QuestionForm(FlaskForm):
    ...
    choices = SelectMultipleField('answer', choices=[('nv', 'Nevada'), ('cl','California'),('la','Los Angeles'], validators=[DataRequired()]) 
    ...
    submit = SubmitField('submit')

In order to make it fancier I have tried to make a drop down menu for the choices, my index.html is as follow:

{% extends "base.html" %}


{% block app_content %}
    <h1>Hi, {{ current_user.username }}!</h1>
    ...
    <form action="", method="post">
      {{ form.hidden_tag() }}
      ...
      {{ form.choices.label }} <br>

        <div class="row"> 
          <select class="selectpicker" multiple data-live-search="true" data-actions-box="true">
            {% for values in form.choices %}
            <p> {{ values }}</p>
            {% endfor %}
          </select>
        </div> 
      </p>
      <p>
        {{ form.submit() }}
      </p>
    </form>

{% endblock %}
...

the problem is I don't know how to pass the choices to my route in flask. currently my route.py is like this:

def myroute():
    form = QuestionForm()
    if form.validate_on_submit():
        ...
        choices=list(filter(None, request.form.getlist('choices')))
        for item in choices:
            choice = Choices(choice=item, ...)
            db.session.add(choice)
            db.session.commit()
       ...

smhquery
  • 98
  • 6

1 Answers1

1

you need to set a name for your select field. Change your index.html to this:

<select class="selectpicker" name='choices' multiple data-live-search="true" data-actions-box="true">
{% for values in form.choices %}
{{ values }} 
{% endfor %}
smhquery
  • 98
  • 6
curious
  • 26
  • 3