嘿,我在做这个小项目时遇到麻烦。
基本上,我正在尝试组织治疗师和他的病人。我有两种数据库模型,治疗师和患者。治疗师注册或登录,我同时有一个治疗师注册表格和一个登录表格。
治疗师成功登录或注册后,将转到首页,他可以单击链接来注册患者。我也已经设置了patientregistrationform
。
我在两件事上遇到麻烦。
在我的项目中,一旦运行它,然后在注册表单上输入字段以创建用户/治疗师,就会出现错误。我相信这是模型类中的错误,但我不知道为什么。
我一周前才开始这样做,所以,如果有人可以帮助我,我将非常感激。
sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) no such table: user [SQL: 'SELECT user.id AS user_id, user.username AS user_username, user.email AS user_email, user.password_hash AS user_password_hash \nFROM user \nWHERE user.username = ?\n LIMIT ? OFFSET ?'] [parameters: ('yohannes', 1, 0)] (Background on this error at: http://sqlalche.me/e/e3q8)
这是我的模型文件
from app import db, login
from flask_login import UserMixin
from werkzeug.security import generate_password_hash, check_password_hash
class User(UserMixin, db.Model):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(64), index=True, unique=True)
email = db.Column(db.String(120), index=True, unique=True)
password_hash = db.Column(db.String(128))
patients = db.relationship('Patient', backref='author', lazy='dynamic')
def __repr__(self):
return '<User {}>'.format(self.username)
def set_password(self, password):
self.password_hash = generate_password_hash(password)
def check_password(self, password):
return check_password_hash(self.password_hash, password)
@login.user_loader
def load_user(id):
return User.query.get(int(id))
class Patient(db.Model):
id = db.Column(db.Integer, primary_key=True)
firstname = db.Column(db.String(64))
lastname = db.Column(db.String(64))
dateofbirth = db.Column(db.String(32))
typeoftherapy = db.Column(db.String(64))
user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
def __repr__(self):
return '<Patient {}>'.format(self.firstname)
这是我的表单文件
from flask_wtf import FlaskForm
from wtforms import StringField, PasswordField, BooleanField, SubmitField
from wtforms.validators import ValidationError, DataRequired, EqualTo, Email
from app.models import User, Patient
class PatientSignUpForm(FlaskForm):
firstname = StringField('First Name', validators=[DataRequired()])
lastname = StringField('Last Name', validators=[DataRequired()])
dateofbirth = StringField('Date of Birth', validators=[DataRequired()])
typeoftherapy = StringField('Type of Therapy', validators=[DataRequired()])
submit = SubmitField('Sign Up Patient')
class TherapistLoginForm(FlaskForm):
username = StringField('Username', validators=[DataRequired()])
password = PasswordField('Password', validators=[DataRequired()])
remember_me = BooleanField('Remember Me')
submit = SubmitField('Sign In')
class TherapistRegistrationForm(FlaskForm):
username = StringField('Username', validators=[DataRequired()])
email = StringField('Email', validators=[DataRequired(), Email()])
password = PasswordField('Password', validators=[DataRequired()])
password2 = PasswordField(
'Repeat Password', validators=[DataRequired(), EqualTo('password')])
submit = SubmitField('Register')
def validate_username(self, username):
user = User.query.filter_by(username=username.data).first()
if user is not None:
raise ValidationError('Please use a different username.')
def validate_email(self, email):
user = User.query.filter_by(email=email.data).first()
if user is not None:
raise ValidationError('Please use a different email address.')
这是我的路线文件
from flask import render_template, flash, redirect, url_for, request
from flask_login import login_user, logout_user, current_user, login_required
from werkzeug.urls import url_parse
from app import app, db
from app.forms import PatientSignUpForm, TherapistLoginForm, TherapistRegistrationForm
from app.models import Patient, User
@app.route('/')
@app.route('/index')
@login_required
def index():
patients = [
{
'doctor': {'username': 'Yohannes'},
'treatment': 'Physical Therapy'
},
{
'doctor': {'username': 'Henok'},
'treatment': 'Mental Therapy'
}
]
return render_template('index.html', title ='Home', user=user, patients=patients)
@app.route('/patients')
def patients():
patient = Patient.query.filter_by(firstname=firstname).all()
patients = [
{'Patient': patient}, {'Patient': patient}
]
return render_template('base.html', patient=patient)
@app.route('/signup', methods=['GET', 'POST'])
def login():
if current_user.is_authenticated:
return redirect(url_for('index'))
form = TherapistLoginForm()
if form.validate_on_submit():
user = Therapist.query.filter_by(username=form.username.data).first()
if user is None or not Therapist.check_password(form.password.data):
flash('Invalid username or password')
return redirect(url_for('login'))
login_user(user, remember=form.remember_me.data)
next_page = request.args.get('next')
if not next_page or url_parse(next_page).netloc != '':
next_page = url_for('index')
return redirect(next_page)
return render_template('login.html', title='Sign In', form=form)
@app.route('/logout')
def logout():
logout_user()
return redirect(url_for('index'))
@app.route('/register', methods=['GET', 'POST'])
def register():
if current_user.is_authenticated:
return redirect(url_for('index'))
form = TherapistRegistrationForm()
if form.validate_on_submit():
user = User(username=form.username.data, email=form.email.data)
user.set_password(form.password.data)
db.session.add(user)
db.session.commit()
flash('Congratulations, you are now a registered user!')
return redirect(url_for('login'))
return render_template('register.html', title='Register', form=form)
答案 0 :(得分:0)
来自sqlite3的错误非常好,它告诉您no such table: user
的意思是它的意思-数据库中没有名为user
的表。您是否初始化了数据库?你有数据库文件吗?
我建议您使用Flask-Migrate进行数据库初始化和迁移。