【问题标题】:<UnboundField(TextField, ('Study',), {})> where is the problem?<UnboundField(TextField, ('Study',), {})> 问题出在哪里?
【发布时间】:2019-04-30 15:00:19
【问题描述】:

我是 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】:

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

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

    这意味着“看似静态的类将不会被转换”,因为现在避免了所有代码。而是这样做:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-07-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多