flask-wtf无法使用 validate_on_submit()函数

实例化了一个Form 提示我没法使用validate_on_submit()这个函数,也不知道是哪儿的问题。
Form其他的正常,也能渲染出来

class AddlhForm(Form):
    """docstring for addlh"""
    name = TextField('名称',[validators.Required()])
    deduction = IntegerField('扣分')
    submit = SubmitField('submit')
@app.route('/addlh', methods=['GET', 'POST'])
def addlh():
    form = AddlhForm(request.form)
    if form.validate_on_submit():
        name = form.name.data
        deduction = form.deduction.data
        form.name.data = ''
        form.deduction.data = ''
        lh = Liangh(name, deduction)
        db.session.add(lh)
        db.session.commit()

    return render_template('addlh.html', form=form)
        <form method="POST" action="/addlh" class="pure-form">
            <div class="getstu left">
                <div>
                    <span>{{ form.name.label }}</span>
                    <span>{{ form.name() }}</span>
                </div>
                <div>
                    <span>{{ form.deduction.label }}</span>
                    <span>{{ form.deduction() }}</span>
                </div>
                {{form.submit()}}
            </div>
        </form>

然后就会报错
AttributeError
AttributeError: 'AddlhForm' object has no attribute 'validate_on_submit'

阅读 9.6k
2 个回答

看看你引入的form对不对,有可能你是这样引入的

from flask.ext.wtf import Form
from wtforms import Form, TextField, IntegerField, SubmitField

这里第二行的Form会顶替掉第一个wtf的form,所以你可能一直在用wtforms里的form,导致你的form没有validate_on_submit

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题