Saya selesai Bab 8 pada sebelah petang Saya boleh log masuk ke halaman web dan menyimpan data dalam pangkalan data, tetapi saya tidak boleh menghantar mesej e-mel Jadi saya menyalin
kod sumber, dan kini masalah baru telah timbul Perbezaan antara saya dan kod sumber adalah konfigurasi pangkalan data, saya terus menaip alamat, dan ia boleh digunakan pada sebelah petang saya juga menaip alamat e-mel saya. tiada kunci asing yang dikaitkan dengan Pengguna dan Peranan Buku itu menyebut penghijrahan pangkalan data, jadi saya juga mencubanya, tetapi saya tidak pasti ada masalah dengan pangkalan data saya. Terima kasih terlebih dahulu
Error
sqlalchemy.exc.ProgrammingError: (pymysql.err.ProgrammingError) (1146, "Jadual 'flask.users' tidak wujud") [SQL AS: '_ID pengguna. .email AS users_email, users.username AS users_username, users.role_id AS users_role_id, users.password_hash AS users_password_hash, users.disahkan AS pengguna_disahkan nFROM pengguna nWHERE users.id = %(param_1)s'] [parameter: {'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))
Ralat sangat jelas
(pymysql.err.ProgrammingError) (1146, "Jadual 'flask.users' tidak wujud")