python - flask-wtf无法使用 validate_on_submit()函数
ringa_lee
ringa_lee 2017-04-17 14:45:51
[Python讨论组]

实例化了一个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">
            <p class="getstu left">
                <p>
                    <span>{{ form.name.label }}</span>
                    <span>{{ form.name() }}</span>
                </p>
                <p>
                    <span>{{ form.deduction.label }}</span>
                    <span>{{ form.deduction() }}</span>
                </p>
                {{form.submit()}}
            </p>
        </form>

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

ringa_lee
ringa_lee

ringa_lee

全部回复(2)
PHP中文网

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

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

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

高洛峰

不是喔,这个我看到过了,并不是因为这个,还是谢谢了

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号