所以我创建了一个用户模型和一个表单类,以便在创建帐户时接收用户数据。但是当我提交时,一个完整性错误页面弹出并显示下一个错误:
> sqlalchemy.exc.IntegrityError: (sqlite3.IntegrityError) UNIQUE
> constraint failed: user.email_adress [SQL: INSERT INTO user (username,
> email_adress, password_hash, budget) VALUES (?, ?, ?, ?)] [parameters:
> ('erickkpruneda', '[email protected]', 'Guero432', 1000)]
> (Background on this error at: https://sqlalche.me/e/20/gkpj)
字符串
正如你所看到的,这是我在表单上输入的数据。更奇怪的是,当我检查数据库时,数据实际上保存在数据库BC中,它有所有的用户,电子邮件和密码信息。
from market import app, db
from flask import render_template, redirect, url_for
from market.models import Item, User
from market.forms import RegisterForms
@app.route('/') # decorator
def home_page():
return render_template('home.html')
@app.route('/market')
def market():
items = Item.query.all()
return render_template('market.html', items = items)
@app.route('/register', methods = ['GET', 'POST'])
def registerPage():
form = RegisterForms()
if form.validate_on_submit():
user_to_create = User(username = form.username.data, email_adress = form.email.data, password_hash = form.password_1.data)
db.session.add(user_to_create)
db.session.commit()
return redirect(url_for('market'))
return render_template('register.html', form = form)
型
型号:
from market import db
class User(db.Model):
id = db.Column(db.Integer(), primary_key = True)
username = db.Column(db.String(length = 30), nullable = False, unique = True)
email_adress = db.Column(db.String(length = 50), nullable = False, unique = True)
password_hash = db.Column(db.String(length = 60), nullable = False)
budget = db.Column(db.Integer(), nullable = False, default = 1000)
items = db.relationship('Item', backref = 'owned_user', lazy = True)
def __repr__(self) -> str:
return f'User {self.username}'
型
和形式:
from flask_wtf import FlaskForm
from wtforms import StringField, PasswordField, SubmitField
class RegisterForms(FlaskForm):
username = StringField(label= 'Username: ')
email = StringField(label= 'Email address: ')
password_1 = PasswordField(label= 'Password:')
password_2 = PasswordField(label= 'Confirm password: ')
submit = SubmitField(label= 'Create account')
型
1条答案
按热度按时间mspsb9vt1#
尝试在FlaskForm的validate方法中进行邮件验证
字符串