搜尋

首頁  >  問答  >  主體

python - flask拷貝原始碼第八章使用者角色出現錯誤

下午把第八章做完,網頁可以登錄,資料庫也可以存入資料就是無法發送信箱訊息,我於是就拷貝了
源碼,現在出現了新的問題,
我和原始碼不同的是資料庫設定這,我直接寫死地址,下午的時候也是可以用的,我這郵箱也寫死,
之前測試過也沒問題,我之前User和Role沒有關聯外鍵,書上有說資料庫遷移,我也試了,但是不太確認,我想有可能是資料庫這有問題,希望有人能幫下我,在此先謝謝了

錯誤
sqlalchemy.exc.ProgrammingError
sqlalchemy.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 . : {'param_1': 1}]

#coding:utf-8

import os
basedir = 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):
        pass


class 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_hash
from itsdangerous import TimedJSONWebSignatureSerializer as Serializer
from flask import current_app
from flask_login import UserMixin
from . import db, login_manager


class 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.name


class 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 False
        if data.get('confirm') != self.id:
            return False
        self.confirmed = True
        db.session.add(self)
        return True

    def __repr__(self):
        return '<User %r>' % self.username


@login_manager.user_loader
def load_user(user_id):
    return User.query.get(int(user_id))
世界只因有你世界只因有你2752 天前796

全部回覆(1)我來回復

  • 阿神

    阿神2017-05-18 10:49:39

    報錯已經很明確了
    (pymysql.err.ProgrammingError) (1146, "Table 'flask.users' doesn't exist")

    回覆
    0
  • 取消回覆