您的位置:首页技术文章
文章详情页

python - flask拷贝源码第八章用户角色出现错误

【字号: 日期:2022-06-13 10:06:44浏览:19作者:猪猪

问题描述

下午把第八章做完,网页可以登录,数据库也可以存入数据就是无法发邮箱信息,我于是就拷贝了源码,现在出现了新的问题,我和源码不同的是数据库配置这,我直接写死地址,下午的时候也是可以用的,我这邮箱也写死,之前测试过也没问题,我之前User和Role没有关联外键,书上有说数据库迁移,我也试了,但是不太确认,我想有可能是数据库这有问题,希望有人能帮下我,在此先谢谢了

错误sqlalchemy.exc.ProgrammingErrorsqlalchemy.exc.ProgrammingError: (pymysql.err.ProgrammingError) (1146, 'Table ’flask.users’ doesn’t exist') [SQL: ’SELECT users.id AS users_id, users.email AS users_email, users.username AS users_username, users.role_id AS users_role_id, users.password_hash AS users_password_hash, users.confirmed AS users_confirmed nFROM users nWHERE users.id = %(param_1)s’] [parameters: {’param_1’: 1}]

#coding:utf-8import osbasedir = os.path.abspath(os.path.dirname(__file__))class Config: SECRET_KEY = ’hard to guess string’ SSL_DISABLE = False SQLALCHEMY_COMMIT_ON_TEARDOWN = True SQLALCHEMY_TRACK_MODIFICATIONS = False SQLALCHEMY_RECORD_QUERIES = True MAIL_SERVER = ’smtp.163.com’ MAIL_PORT = 25 MAIL_USE_TLS = True MAIL_USERNAME = (’13166337919@163.com’) MAIL_PASSWORD = (’a123123’) FLASKY_MAIL_SUBJECT_PREFIX = ’[Flasky]’ FLASKY_MAIL_SENDER = ’Flasky Admin <13166337919@163.com>’ FLASKY_ADMIN = (’626825701@qq.com’) @staticmethod def init_app(app):passclass DevelopmentConfig(Config): DEBUG = True SQLALCHEMY_DATABASE_URI =’mysql+pymysql://root:123456@127.0.0.1/flask’class TestingConfig(Config): TESTING = True SQLALCHEMY_DATABASE_URI = ’mysql+pymysql://root:123456@127.0.0.1/text’class ProductionConfig(Config): SQLALCHEMY_DATABASE_URI =’mysql+pymysql://root:123456@127.0.0.1/pro’config = { ’development’: DevelopmentConfig, ’testing’: TestingConfig, ’production’: ProductionConfig, ’default’: DevelopmentConfig}

from werkzeug.security import generate_password_hash, check_password_hashfrom itsdangerous import TimedJSONWebSignatureSerializer as Serializerfrom flask import current_appfrom flask_login import UserMixinfrom . import db, login_managerclass Role(db.Model): __tablename__ = ’roles’ id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(64), unique=True) users = db.relationship(’User’, backref=’role’, lazy=’dynamic’) def __repr__(self):return ’<Role %r>’ % self.nameclass User(UserMixin, db.Model): __tablename__ = ’users’ id = db.Column(db.Integer, primary_key=True) email = db.Column(db.String(64), unique=True, index=True) username = db.Column(db.String(64), unique=True, index=True) role_id = db.Column(db.Integer, db.ForeignKey(’roles.id’)) password_hash = db.Column(db.String(128)) confirmed = db.Column(db.Boolean, default=False) @property def password(self):raise AttributeError(’password is not a readable attribute’) @password.setter def password(self, password):self.password_hash = generate_password_hash(password) def verify_password(self, password):return check_password_hash(self.password_hash, password) def generate_confirmation_token(self, expiration=3600):s = Serializer(current_app.config[’SECRET_KEY’], expiration)return s.dumps({’confirm’: self.id}) def confirm(self, token):s = Serializer(current_app.config[’SECRET_KEY’])try: data = s.loads(token)except: return Falseif data.get(’confirm’) != self.id: return Falseself.confirmed = Truedb.session.add(self)return True def __repr__(self):return ’<User %r>’ % self.username@login_manager.user_loaderdef load_user(user_id): return User.query.get(int(user_id))

问题解答

回答1:

报错已经很明确了(pymysql.err.ProgrammingError) (1146, 'Table ’flask.users’ doesn’t exist')

标签: Python 编程
相关文章: