where is the problem?

问题描述 投票:0回答:1

我是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',), {})>
python flask flask-wtforms wtforms
1个回答
0
投票

你有类继承:StudyManagementForm(Form)但你选择通过这样做专门覆盖__init__()方法:

def __init__(self):
    print('a')

这意味着“看似静态的类不会被转换”,因为现在可以避免所有代码。相反:

def __init__(self):
    super().__init__()
    print('a')

现在原来的Form.__init__()将被执行,然后,你的打印声明将被执行。未绑定的字段将在该过程中绑定。

© www.soinside.com 2019 - 2024. All rights reserved.