Я хочу иметь встроенный валидатор для дублированного имени пользователя и электронной почты. однако, это работает только тогда, когда я впервые определяю функцию как функцию и помещаю ее в список валидаторов. Ниже приведены мои коды
Вот колба
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": "your@email.com"})
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": "your@email.com"})
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')
Спасибо.