TypeError:validate_on_submit()缺少1个必需的位置参数:"self" [英] TypeError: validate_on_submit() missing 1 required positional argument: 'self'

查看:65
本文介绍了TypeError:validate_on_submit()缺少1个必需的位置参数:"self"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Flask网站开发教程,但我已经遇到了一些错误;我简单的Web应用程序正在使用FlaskForm并试图验证信息,但是特别是在函数validate_on_submit()上遇到了一些麻烦.这是我的python代码

Am using Flask web development tutorial and I have and am running to several errors; my simple web app is utilizing FlaskForm and am trying to validate the information, but am running into some trouble especially at the function validate_on_submit(). Here is my python code

 class NameForm(FlaskForm):
   name = StringField("What is your name?", validators=[required])
   submit = SubmitField('Submit')



 @app.route('/index', methods=['GET', 'POST'])
 def index():
     name = None
     formi = NameForm()


     if FlaskForm.validate_on_submit():
        name = FlaskForm.name.data
        FlaskForm.name.data = ''
        return render_template('index.html', form=formi, name=name)

我的错误发生在if语句

my error occurs at the if statement

推荐答案

您需要从表单实例而不是针对类调用 validate_on_submit .使用:

You need to call validate_on_submit from your form instance and not for the class. Use:

 if formi.validate_on_submit():

这篇关于TypeError:validate_on_submit()缺少1个必需的位置参数:"self"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆