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()
       ...

0

1 Answer 1

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 %}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.