TypeError: LoginForm.validate() 出现了意外的关键字参数 ‘extra_validators’

huangapple go评论118阅读模式
英文:

Why am i getting this error: TypeError: LoginForm.validate() got an unexpected keyword argument 'extra_validators'

问题

我在尝试使用Flask登录我的应用程序时遇到了这个错误,我正在使用MongoDB作为我的数据库。以下是来自app.py的所有代码:

from flask import Flask, render_template, request, redirect, url_for
from flask_mongoengine import MongoEngine
from flask_security import Security, MongoEngineUserDatastore, UserMixin, RoleMixin, login_required, login_user, logout_user, current_user, LoginForm

app = Flask(__name)
app.config['DEBUG'] = True
app.config['SECRET_KEY'] = 'super-secret-key'
app.config['SECURITY_PASSWORD_SALT'] = 'super-secret-salt'
app.config['MONGODB_SETTINGS'] = {
    'db': 'flask-login',
    'host': 'localhost',
    'port': 27017,
}

db = MongoEngine(app)

class Role(db.Document, RoleMixin):
    name = db.StringField(max_length=80)
    description = db.StringField(max_length=255)

class User(db.Document, UserMixin):
    name = db.StringField(max_length=255)
    email = db.StringField(max_length=255, unique=True)
    password = db.StringField(max_length=255)
    active = db.BooleanField(default=True)
    confirmed_at = db.DateTimeField()
    roles = db.ListField(db.ReferenceField(Role), default=[])

user_datastore = MongoEngineUserDatastore(db, User, Role)
security = Security(app, user_datastore)

@app.route('/dashboard')
@login_required
def dashboard():
    return render_template('dashboard.html')

@app.route('/')
def home():
    return render_template('home.html')

@app.route('/register', methods=['POST'])
def add_user():
    name = request.form['name']
    email = request.form['email']
    password = request.form['password']
    user = User(name=name, email=email, password=password)
    user.save()
    return redirect('/')

@app.route('/logout')
@login_required
def logout():
    logout_user()
    return redirect('/')

我尝试创建不同的登录路由,但没有成功。我的问题的唯一“解决办法”是从flask_wtf包的form.py文件中删除"extra_validators=None",从以下修改前的代码:

def validate_on_submit(self, extra_validators=None):
    """Call :meth:`validate` only if the form is submitted.
    This is a shortcut for ``form.is_submitted() and form.validate()``.
    """
    return self.is_submitted() and self.validate(extra_validators=extra_validators)

到以下修改后的代码:

def validate_on_submit(self):
    """Call :meth:`validate` only if the form is submitted.
    This is a shortcut for ``form.is_submitted() and form.validate()``.
    """
    return self.is_submitted() and self.validate()

然后登录就能正常工作,但我不确定是否修改了包文件是否正确。

英文:

I am get
ting t
his error when i try to log in my application with flask, i am using mongodb for my database. This is all the code from app.py

from flask import Flask, render_template, request, redirect, url_for
from flask_mongoengine import MongoEngine
from flask_security import Security, MongoEngineUserDatastore, UserMixin, RoleMixin, login_required, login_user, logout_user, current_user, LoginForm
app = Flask(__name__)
app.config['DEBUG'] = True
app.config['SECRET_KEY'] = 'super-secret-key'
app.config['SECURITY_PASSWORD_SALT'] = 'super-secret-salt'
app.config['MONGODB_SETTINGS'] = {
'db': 'flask-login',
'host': 'localhost',
'port': 27017,
}
db = MongoEngine(app)
class Role(db.Document, RoleMixin):
name = db.StringField(max_length=80)
description = db.StringField(max_length=255)
class User(db.Document, UserMixin):
name = db.StringField(max_length=255)
email = db.StringField(max_length=255, unique=True)
password = db.StringField(max_length=255)
active = db.BooleanField(default=True)
confirmed_at = db.DateTimeField()
roles = db.ListField(db.ReferenceField(Role), default=[])
user_datastore = MongoEngineUserDatastore(db, User, Role)
security = Security(app, user_datastore)
@app.route('/dashboard')
@login_required
def dashboard():
return render_template('dashboard.html')
@app.route('/')
def home():
return render_template('home.html')
@app.route('/register', methods=['POST'])
def add_user():
name = request.form['name']
email = request.form['email']
password = request.form['password']
user = User(name=name, email=email, password=password)
user.save()
return redirect('/')
@app.route('/logout')
@login_required
def logout():
logout_user()
return redirect('/')

I tried to do a diferent login route but no success. The only "solution" to my problem was deleting "extra_validators=None" from the "validate_on_submit" function on the form.py file from flask_wtf package.

From this:

def validate_on_submit(self, extra_validators=None):
"""Call :meth:`validate` only if the form is submitted.
This is a shortcut for ``form.is_submitted() and form.validate()``.
"""
return self.is_submitted() and self.validate(extra_validators=extra_validators)

To this:

def validate_on_submit(self):
"""Call :meth:`validate` only if the form is submitted.
This is a shortcut for ``form.is_submitted() and form.validate()``.
"""
return self.is_submitted() and self.validate()

Then the login worked but i don't know if is right to modificate the package file

答案1

得分: 1

确保WTForms、Flask-WTForms和Flask-Security-Too都是最新版本。应该都能正常工作。
如果仍然有问题,请确切地发布这些包的版本信息。

英文:

Make sure WTForms, Flask-WTForms, and Flask-Security-Too are all at the latest version. It should all work.
If you still have issues - please post precisely what versions of these packages you are using.

huangapple
  • 本文由 发表于 2023年3月7日 03:45:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/75655188.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定