5
12

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Flask-WTF で FieldList, FormField を利用してフォームを複製する

Last updated at Posted at 2017-08-10

Flaskでのフォーム作成にはFlask-WTFを利用するとWTFormのバリデーションの利用に加えてCSRFトークンを生成・検証してくれます。

フォームを表などで用意するときは行単位で複製できると便利なのですが、Flask-WTFでこれを行う場合は少し注意が必要です。
複製する子フォームではCSRFトークンを生成しないようにする必要があります。
Screen Shot 2017-08-11 at 2.12.14.png

app.py
from flask import Flask, render_template, request
from flask_wtf import FlaskForm
from wtforms import FormField, FieldList, IntegerField, StringField, SubmitField
from wtforms.validators import Required

app = Flask(__name__)
app.secret_key = 'HardToGuess'

class MemberForm(FlaskForm):
    def __init__(self, *args, **kwargs):
        kwargs['csrf_enabled'] = False  # 子フォームではCSRFトークンが生成されないように設定
        super(MemberForm, self).__init__(*args, **kwargs)

    m_name = StringField('name', validators=[Required()])
    score = IntegerField('id', validators=[Required()])


class TeamForm(FlaskForm):
    members = FieldList(FormField(MemberForm, 'Member'), min_entries=2, max_entries=10)
    """
    FieldlistとFormFieldを用いて子フォーム複製
    """
    submit = SubmitField('送信')


@app.route('/', methods=['POST','GET'])
def index():
    form = TeamForm()
    print(request.form)  # チェック用
    if form.validate_on_submit():
        print('Validated')
        print(form.members.data)
    else:
        print('NOT Validated')
    return render_template('index.html', form=form)


if __name__ == '__main__':
    app.run(debug=True)
index.html
<form action="" method="post">
    {{ form.csrf_token() }}
    <h1>Flask-WTF Fieldlistサンプル</h1>
    <table>
        <tr>
            <td>ID</td>
            <td>名前</td>
            <td>点数</td>
    {% for member in form.members %}
        <tr>
            <td>{{ loop.index }}</td>
            <td>{{ member.m_name() }}</td>
            <td>{{ member.score() }}</td>
        </tr>
    {% endfor %}
    </table>
    {{ form.submit() }}
</form>

flask_wtf.FlaskForm からではなく、 wtf.Formを継承して作成しても同じことができそうです。

5
12
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
12

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?