我想要一个用于重复的用户名和电子邮件的内联验证器。但是,它仅在我首先将is定义为一个函数并将其放入验证器列表时才起作用。以下是我的代码
这是烧瓶
from project import app, db
from project.forms import Reg_form
from project.models import User
from flask import render_template, flash, session, redirect, url_for
from flask_login import login_required
@app.route('/register', methods=['GET','POST'])
def register():
form = Reg_form()
if form.validate_on_submit():
new_user = User(email = form.email.data,
username = form.username.data,
password = form.password.data)
db.session.add(new_user)
db.session.commit()
flash('Thanks for registering, you may login now.')
return redirect(url_for('home'))
return render_template('register.html', form = form)
if __name__ == '__main__':
app.run(debug=True)
这是表格
from flask_wtf import FlaskForm
from wtforms import StringField, IntegerField, SubmitField, PasswordField
from wtforms.validators import data_required, email, equal_to, length, ValidationError
from project.models import User
def check_email(self, email):
if User.query.filter_by(email=email.data).first():
raise ValidationError('Email has been registered')
def check_username(self, username):
if User.query.filter_by(username=username.data).first():
raise ValidationError('Username has been registered')
class Reg_form(FlaskForm):
email = StringField('Email', validators=[data_required(), email(), check_email], render_kw={"placeholder": "[email protected]"})
username = StringField('Username', validators=[data_required(), check_username], render_kw={"placeholder": "Your username"})
password = PasswordField('Password', validators=[data_required(), equal_to('confirm_pw', message='Password must match with the confirmed password.')], render_kw={"placeholder": "Your password"})
confirm_pw = PasswordField('Confirm password', validators=[data_required()], render_kw={"placeholder": "Confirm your password"})
submit = SubmitField('Register.')
这是我想要的但不起作用
from flask_wtf import FlaskForm
from wtforms import StringField, IntegerField, SubmitField, PasswordField
from wtforms.validators import data_required, email, equal_to, length, ValidationError
from project.models import User
class Reg_form(FlaskForm):
email = StringField('Email', validators=[data_required(), email()], render_kw={"placeholder": "[email protected]"})
username = StringField('Username', validators=[data_required()], render_kw={"placeholder": "Your username"})
password = PasswordField('Password', validators=[data_required(), equal_to('confirm_pw', message='Password must match with the confirmed password.')], render_kw={"placeholder": "Your password"})
confirm_pw = PasswordField('Confirm password', validators=[data_required()], render_kw={"placeholder": "Confirm your password"})
submit = SubmitField('Register.')
def check_email(self, email):
if User.query.filter_by(email=email.data).first():
raise ValidationError('Email has been registered')
def check_username(self, username):
if User.query.filter_by(username=username.data).first():
raise ValidationError('Username has been registered')
谢谢。
这是因为自定义验证器必须以开头validate_
。实际上,我发现很难在文档中找到确切的位置(例如,这里没有明确提及我认为值得提及的地方),但是您可以在实现中看到它:
def validate(self):
"""
Validates the form by calling `validate` on each field, passing any
extra `Form.validate_<fieldname>` validators to the field validator.
"""
extra = {}
for name in self._fields:
inline = getattr(self.__class__, "validate_%s" % name, None)
if inline is not None:
extra[name] = [inline]
return super(Form, self).validate(extra)
因此,您需要更改check_email
为validate_email
etc。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句