将额外的参数传递给wtforms

时间:2019-05-20 12:13:51

标签: python flask wtforms

我正在尝试将一些额外的参数传递给wtforms生成的form。 问题是我无法访问表单类中的额外数据。

  from flask import redirect, render_template, request, session, url_for
  from .forms import TestForm

  @bp.route('/test', methods=('GET', 'POST'))
    def test():
      user_id = session.get('user_id')
      user = User.query.get(user_id)
      form = TestForm(request.form, user)

      if request.method == 'POST' and form.validate():
        return redirect(url_for('user.settings'))
      return render_template('user/settings.html', user=user, form=form)

这是表单类本身:

from wtforms import Form, StringField

class TestForm(Form):
    def __init__(self, custom_data, *args, **kwargs):
        super().__init__(*args, **kwargs)
        print(custom_data) # Can't access!

    # Can't access!

    username = StringField('Username')

0 个答案:

没有答案