flask - FlaskForm 不接受字段名称“进程”

当我将字段名称设置为“处理”时,Flask 无法处理我的表单。这是示例代码:

from flask import Flask, render_template_string
from flask_wtf import FlaskForm
from wtforms import StringField

class MyForm(FlaskForm):
    process = StringField('My StringField')

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

@app.route('/', methods=['GET', 'POST'])
def index():
    form = MyForm()
    return render_template_string(f'<div>{form.process.label}{form.process}</div>')
app.run(port=5000, debug=True)

上面的代码给出错误为

/home/flask/.env/lib/python3.9/site-packages/wtforms/form.py", line 208, in __call__
    return type.__call__(cls, *args, **kwargs)
  File "/home/flask/.env/lib/python3.9/site-packages/flask_wtf/form.py", line 73, in __init__
    super().__init__(formdata=formdata, **kwargs)
  File "/home/flask/.env/lib/python3.9/site-packages/wtforms/form.py", line 286, in __init__
    self.process(formdata, obj, data=data, **kwargs)
TypeError: __call__() takes 1 positional argument but 3 were given

如果我将字段从 process = StringField('My StringField') 重命名为任何其他名称,例如myprocess。这是为什么?如何摆脱它?

回答1

form.process() 实际上是 Flask-forms 中的一个保留函数,用于在初始化期间必须定义默认的 values 时使用。您应该为表单使用其他字段名称,例如 process_。

相似文章