简体   繁体   中英

Flask-WTForms form.validate() fails on dynamic choices

I'm creating a survey application that displays the survey question and choices and allows the user to pick a choice through the Flask-WTForms package. The form uses a RadioField and seems to fail form.validate() when populating the choices attribute dynamically.

When I manually enter in the choices as such:

class SurveyAnswerForm(FlaskForm):
    answers = RadioField('Answers',
                         coerce=str,
                         choices=[('18-25', '18-25'), ('26-35', '26-35')])

form.validate() returns True and there are no errors in form.error .

When I decide to populate the choices attribute dynamically (see below), form.validate() returns False and form.error returns:

{'answers': ['Not a valid choice']}.

I've been working at this for hours and am not sure why form.validate() returns False .

forms.py :

from flask_wtf import FlaskForm
from wtforms import RadioField

class SurveyAnswerForm(FlaskForm):
    answers = RadioField('Answers',
                         coerce=str,
                         choices=[])

app.py :

@app.route('/survey/<int:survey_id>/questions', methods=['GET', 'POST'])
def survey_questions(survey_id):
    survey = Survey.query.filter_by(id=survey_id).first()
    page = request.args.get('page', 1, type=int)
    questions = SurveyQuestion.query.filter_by(survey_id=survey_id)\
        .order_by(SurveyQuestion.id)\
        .paginate(page, 1, True)

    for question in questions.items:
        question_id = question.id

    choices = QuestionChoices.query\
        .join(SurveyQuestion,
              and_(QuestionChoices.question_id==question_id,
               SurveyQuestion.survey_id==survey_id)).all()

    form = SurveyAnswerForm(request.form)
    form.answers.choices = [(choice.choice, choice.choice)\
        for choice in choices]

    if request.method =='POST' and form.validate():
        print('Successful POST')

    next_url = url_for('survey_questions', survey_id=survey.id,
                       page=questions.next_num)\
        if questions.has_next else None
    prev_url = url_for('survey_questions', survey_id=survey.id,
                   page=questions.prev_num)\
        if questions.has_prev else None

    return render_template('survey_question.html',
                           survey=survey,
                           questions=questions.items,
                           choices=choices,
                           form=form,
                           next_url=next_url, prev_url=prev_url)

survey_question.html :

{% extends "layout.html" %}
{% block body %}
  <h2>{{ survey.survey_title }}</h2>
  {% for question in questions %}
    <h3>{{ question.question }}</h3>
  {% endfor %}

  <form action="{{ next_url }}" method="POST">
    {{ form.csrf_token }}
    {{ form.answers(name='answer') }}

    {% if prev_url %}
      <a href="{{ prev_url }}">Back</a>
    {% endif %}
    {% if next_url %}
      <input type="submit" value="Continue">
   {% else %}
      <a href="#">Finish</a>
    {% endif %}
  </form>
{% endblock %}

The problem was submitting a POST request with pagination. if the current link is /survey/1/question?page=2 the form will submit to /submit/1/question?page=3 . To remedy this, I just created a separate route for submission and handled logic there.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM