簡體   English   中英

Flask-WTForms form.validate()在動態選擇上失敗

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

我正在創建一個顯示調查問題和選擇的調查應用程序,並允許用戶通過Flask-WTForms包選擇一個選項。 該表單使用RadioField並且在動態填充choices屬性時似乎會失敗form.validate()

當我手動輸入以下選項時:

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

form.validate()返回True,並且form.error中沒有錯誤。

當我決定動態填充choices屬性時(請參見下文), form.validate()返回False,form.error返回:

{'答案':['無效的選擇']}。

我已經為此工作了幾個小時,並且不確定為什么form.validate()返回False

form.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 %}

問題是通過分頁提交POST請求。 如果當前鏈接為/survey/1/question?page=2則表單將提交給/submit/1/question?page=3 為了解決這個問題,我只是創建了一條單獨的提交路徑並在那里處理了邏輯。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM