【问题标题】:How to display FormField content on html page?如何在 html 页面上显示 FormField 内容?
【发布时间】:2021-03-17 15:43:10
【问题描述】:

我正在创建一个竞赛表单,希望用户在其中添加问题。如果用户点击添加问题按钮,问题将被添加。

我在 forms.py 中尝试过这个

class CompetitionEntryForm(FlaskForm):
    question = StringField('Question', validators=[DataRequired()])
    option1 = StringField('Option 1', validators=[DataRequired()])
    option2 = StringField('Option 2', validators=[DataRequired()])
    option3 = StringField('Option 3', validators=[DataRequired()])
    answer = StringField('Answer', validators=[DataRequired(), Length(min=1, max=2)])

class CompetitionForm(FlaskForm):
    competition = FieldList(FormField(CompetitionEntryForm), min_entries=1)
    submit = SubmitField('Host')

main.py 代码:

@app.route("/competition", methods=['GET', 'POST'])
def competition():
    user_competition = [{"name": "First Address"},
                  {"name": "Second Address"}]
    form = CompetitionForm(competition=user_competition)
    return render_template('competition.html', title='Competition', form=form)

competition.html 代码:

<div class="form-group">
    {% for competition_entry_form in form.competition %}
        {{ competition_entry_form.hidden_tag() }}
        {{ competition_entry_form.name }}
        <!-- There must be some code here to display the form. But I don't know how to do that -->
    {% endfor %}
</div>

但是这个问题我不知道怎么在网页上显示。

提前致谢。

【问题讨论】:

    标签: python html flask flask-wtforms


    【解决方案1】:

    这是我所做的:

    <div class="form-group">
        {% for competition_entry_form in form.competition %}
            {{ competition_entry_form.hidden_tag() }}
            <!-- Add this from below -->
            {{ competition_entry_form.question }}
            {{ competition_entry_form.option1 }}
            {{ competition_entry_form.option2 }}
            <!-- and so on -->
        {% endfor %}
    </div>
    

    【讨论】:

      猜你喜欢
      • 2017-03-07
      • 1970-01-01
      • 1970-01-01
      • 2015-05-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-27
      • 2015-09-13
      相关资源
      最近更新 更多