我是Python的新手,我想用Wtforms创建一个简单的页面,但是这段代码给了我UnboundField错误。 有人可以帮我解决问题吗?
谢谢
from flask_wtf import Form
from wtforms import StringField
from wtforms import TextField
from wtforms import SelectField
from wtforms import RadioField
from wtforms import DecimalField
from wtforms import SubmitField
from datetime import datetime
from flask import render_template
from FlaskWebProject1 import app
class StudyManagementForm(Form):
"""This seemingly static class will be transformed
by the WTForms metaclass constructor"""
study = TextField("Study")
active = RadioField("Etude active")
submit = SubmitField("Ok")
def __init__(self):
print ('a')
@app.route('/')
@app.route('/study_management', methods=['GET', 'POST'])
def study_management():
submitForm = StudyManagementForm()
return render_template(
'study_management.html',
form = submitForm
)
我收到UnboundField错误:
<UnboundField(TextField, ('Study',), {})>
<UnboundField(RadioField, ('Etude active',), {})>
答案 0 :(得分:0)
您具有类继承:StudyManagementForm(Form)
,但您选择通过以下方式专门覆盖__init__()
方法:
def __init__(self):
print('a')
这意味着“看似静态的类将不会被转换”,因为现在避免了所有代码。而是:
def __init__(self):
super().__init__()
print('a')
现在将执行原始的Form.__init__()
,然后执行打印语句。未绑定的字段将在该过程中绑定。