Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
780 views
in Technique[技术] by (71.8m points)

flask自定义验证函数,位置参数缺失

flask_wtf自定义表单验证函数:

def validate_username(self, name):
    if not DB.check_user(name.data):
        raise ValidationError('用户名已存在!')

DB是从db包里导入的数据库类,check_user定义如下:

def check_user(self, name):
    return self.collection_user.find({'username': name}).count() == 0

然后注册相同用户名,报错如下:
if not DB.check_user(name.data):这一行
TypeError: check_user() missing 1 required positional argument: 'name'

name传入了,在验证函数里dir(DB)DB也没问题额,怎么回事,求指导。


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

check_user 函数的第一个函数是self,可以看出来应该是一个类的成员函数,你在调用的时候当作静态函数了。


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...