林妙可减肥夜跑原创文学网 - 纯净的绿色文学家园 !

林妙可减肥夜跑(全文在线阅读>

林妙可减肥夜跑_Flask开发微电影网站(二)

1.安装数据库连接依赖包

pip install flask-sqlalchemy

2.创建movie数据库

在CentOS虚拟机,进入MaridDB数据库提示符,创建movie数据库

create database movie

3.定义MaridDB数据库连接

在app目录下的__init__.py文件中添加以下内容

# _*_coding:utf-8_*_

from flask import Flask, render_template
from flask_sqlalchemy import SQLAlchemy
import pymysql

# 实例化Flask类得到app对象
app = Flask(__name__)

# 定义连接的数据库
app.config["SQLALCHEMY_DATABASE_URI"] = "mysql+pymysql://root:root1234@192.168.81.10:3306/movie"

# 如果设置成True(默认情况),Flask-SQLAlchemy将会追踪对象的修改并且发送信号
# 这需要额外的内存,如果不必要的可以禁用
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = True

# 定义secret_key
app.config["SECRET_KEY"] = "6bd749587aad49399f674b202a07d56f"

db = SQLAlchemy(app)

4.定义数据模型类

在app目录下创建models.py文件,文件内容为

# _*_coding:utf-8_*_

from datetime import datetime
from app import db

4.1 定义会员数据模型

# 会员
class User(db.Model):
    __tablename__ = "user"      # 定义用户表在数据库中的名称
    id = db.Column(db.Integer, primary_key=True)    # 用户编号
    name = db.Column(db.String(100), unique=True)   # 呢称
    pwd = db.Column(db.String(100))     # 密码
    email = db.Column(db.String(100), unique=True)  # 邮箱
    phone = db.Column(db.String(11), unique=True)   # 用户手机号码
    info = db.Column(db.Text)           # 个性简介
    face = db.Column(db.String(255), unique=True)   # 头像
    addtime = db.Column(db.DateTime, index=True, default=datetime.utcnow)  # 注册时间
    uuid = db.Column(db.String(255), unique=True)   # 唯一标识
    userlogs = db.relationship("Userlog", backref="user")       # 会员登录日志外键关系关联
    comments = db.relationship("Comment", backref="user")       # 评论外键关系关联
    moviecols = db.relationship("Moviecol", backref="user")     # 电影收藏外键关系关联

    # 返回定义类型
    def __repr__(self):
        return "" % self.name

    # 检测密码,判断用户输入的密码是否与数据库中保存的用户的密码相同
    def check_pwd(self,pwd):
        from werkzeug.security import check_password_hash
        return check_password_hash(self.pwd,pwd)

4.2 定义会员登录日志数据模型

# 会员登录日志
class Userlog(db.Model):
    __tablename__ = "userlog"        # 定义用户日志表在数据库中的名称
    id = db.Column(db.Integer, primary_key=True)    # 编号
    user_id = db.Column(db.Integer, db.ForeignKey("user.id"))  # 所属会员编号
    ip = db.Column(db.String(100))                  # 最近登录IP地址
    addtime = db.Column(db.DateTime, index=True, default=datetime.now)  # 最近登录时间

    def __repr__(self):
        return "" % self.id

4.3 定义标签数据模型

# 电影标签
class Tag(db.Model):
    __tablename__ = "tag"              # 定义电影标签表在数据库中的名称
    id = db.Column(db.Integer, primary_key=True)  # 电影编号
    name = db.Column(db.String(100), unique=True)  # 标题
    addtime = db.Column(db.DateTime, index=True, default=datetime.now)  # 电影添加时间
    movies = db.relationship("Movie", backref="tag")  # 电影外键的键值

    def __repr__(self):
        return "" % self.name
        

4.4 定义电影数据模型

# 电影
class Movie(db.Model):
    __tablename__ = "movie"              # 定义电影表在数据库中的名称
    id = db.Column(db.Integer, primary_key=True)  # 编号
    title = db.Column(db.String(255), unique=True)  # 标题
    url = db.Column(db.String(255), unique=True)  # 地址
    info = db.Column(db.Text)  # 简介
    logo = db.Column(db.String(255), unique=True)  # 封面
    star = db.Column(db.SmallInteger)  # 星级
    playnum = db.Column(db.BigInteger)  # 播放量
    commentnum = db.Column(db.BigInteger)  # 评论量
    tag_id = db.Column(db.Integer, db.ForeignKey("tag.id"))  # 所属标签
    area = db.Column(db.String(255))  # 上映地区
    release_time = db.Column(db.Date)  # 上映时间
    length = db.Column(db.String(100))  # 播放时间
    addtime = db.Column(db.DateTime, index=True, default=datetime.now)  # 添加时间
    comments = db.relationship("Comment", backref="movie")  # 评论外键关系关联
    moviecols = db.relationship("Moviecol", backref="movie")  # 收藏外键关系关联

    def __repr__(self):
        return "" % self.title

4.5 定义上映预告数据模型

# 上映预告
class Preview(db.Model):
    __tablename__ = "preview"              # 定义电影上映预告表在数据库中的名称
    id = db.Column(db.Integer, primary_key=True)  # 编号
    title = db.Column(db.String(255), unique=True)  # 标题
    logo = db.Column(db.String(255), unique=True)  # 封面
    addtime = db.Column(db.DateTime, index=True, default=datetime.now)  # 添加时间

    def __repr__(self):
        return "" % self.title
        

4.6 定义电影评论数据模型

# 电影评论
class Comment(db.Model):
    __tablename__ = "comment"              # 定义电影评论表在数据库中的名称
    id = db.Column(db.Integer, primary_key=True)  # 编号
    content = db.Column(db.Text)  # 评论内容
    movie_id = db.Column(db.Integer, db.ForeignKey("movie.id"))  # 所属电影
    user_id = db.Column(db.Integer, db.ForeignKey("user.id"))  # 所属用户
    addtime = db.Column(db.DateTime, index=True, default=datetime.now)  # 添加时间

    def __repr__(self):
        return "" % self.id
        

4.7 定义电影收藏数据模型

# 电影收藏
class Moviecol(db.Model):
    __tablename__ = "moviecol"              # 定义电影收藏表在数据库中的名称
    id = db.Column(db.Integer, primary_key=True)  # 编号
    movie_id = db.Column(db.Integer, db.ForeignKey("movie.id"))  # 电影编号
    user_id = db.Column(db.Integer, db.ForeignKey("user.id"))  # 用户编号
    addtime = db.Column(db.DateTime, index=True, default=datetime)  # 添加收藏时间

    def __repr__(self):
        return "" % self.id
    

4.8 定义权限数据模型

# 权限
class Auth(db.Model):
    __tablename__ = "auth"              # 定义用户权限表在数据库中的名称
    id = db.Column(db.Integer, primary_key=True)  # 编号
    name = db.Column(db.String(100), unique=True)  # 权限名称
    url = db.Column(db.String(100), unique=True)  # 地址
    addtime = db.Column(db.DateTime, index=True, default=datetime.now)  # 添加时间

    def __repr__(self):
        return "" % self.name
    

4.9 定义角色数据模型

# 角色
class Role(db.Model):
    __tablename__ = "role"              # 定义用户角色表在数据库中的名称
    id = db.Column(db.Integer, primary_key=True)  # 编号
    name = db.Column(db.String(128), unique=True)  # 角色名称
    auths = db.Column(db.String(512))
    addtime = db.Column(db.DateTime, index=True, default=datetime.now)  # 添加时间
    admins = db.relationship("Admin", backref="role")  # 管理员外键关系关联

    def __repr__(self):
        return "" % self.name
    

4.10 定义管理员数据模型

# 管理员
class Admin(db.Model):
    __tablename__ = "admin"              # 定义管理员表在数据库中的名称
    id = db.Column(db.Integer, primary_key=True)  # 编号
    name = db.Column(db.String(100), unique=True)  # 管理员账号
    pwd = db.Column(db.String(100))  # 管理员密码
    is_super = db.Column(db.SmallInteger)  # 是否为超级管理员,0为超级管理员
    role_id = db.Column(db.Integer, db.ForeignKey("role.id"))  # 所属角色
    addtime = db.Column(db.DateTime, index=True, default=datetime.now)
    adminlogs = db.relationship("Adminlog", backref="admin")  # 管理员登录日志外键关系关联
    oplogs = db.relationship("Oplog", backref="admin")  # 管理员操作日志外键关系关联

    def __repr__(self):
        return "" % self.name

    def check_pwd(self, pwd):
        from werkzeug.security import check_password_hash
        return check_password_hash(self.pwd, pwd)
    

4.11 定义管理员登录日志数据模型

# 管理员登录日志
class Adminlog(db.Model):
    __tablename__ = "adminlog"              # 定义管理员日志表在数据库中的名称
    id = db.Column(db.Integer, primary_key=True)  # 编号
    admin_id = db.Column(db.Integer, db.ForeignKey("admin.id"))  # 所属管理员
    ip = db.Column(db.String(100))  # 登录IP
    addtime = db.Column(db.DateTime, index=True, default=datetime.now)  # 登录时间

    def __repr__(self):
        return "" % self.id
        

4.12 定义用户操作日志

# 操作日志
class Oplog(db.Model):
    __tablename__ = "oplog"              # 定义操作日志表在数据库中的名称
    id = db.Column(db.Integer, primary_key=True)  # 编号
    admin_id = db.Column(db.Integer, db.ForeignKey("admin.id"))  # 所属管理员
    ip = db.Column(db.String(100))  # 登录IP
    reason = db.Column(db.String(600))  # 操作原因
    addtime = db.Column(db.DateTime, index=True, default=datetime.now)  # 登录时间

    def __repr__(self):
        return "" % self.id

在models.py文件保存所有的模型类

_ _coding: utf8_ _ from datetime import datetime from app import db 4. 1 ding yi hui yuan shu ju mo xing hui yuan class User db. Model: __tablename__ " user" ding yi yong hu biao zai shu ju ku zhong de ming cheng id db. Column db. Integer, primary_key True yong hu bian hao name db. Column db. String 100, unique True ne cheng pwd db. Column db. String 100 mi ma email db. Column db. String 100, unique True you xiang phone db. Column db. String 11, unique True yong hu shou ji hao ma info db. Column db. Text ge xing jian jie face db. Column db. String 255, unique True tou xiang addtime db. Column db. DateTime, index True, default datetime. utcnow zhu ce shi jian uuid db. Column db. String 255, unique True wei yi biao shi userlogs db. relationship" Userlog", backref" user" hui yuan deng lu ri zhi wai jian guan xi guan lian comments db. relationship" Comment", backref" user" ping lun wai jian guan xi guan lian moviecols db. relationship" Moviecol", backref" user" dian ying shou cang wai jian guan xi guan lian fan hui ding yi lei xing def __repr__ self: return " lt User r gt" self. name jian ce mi ma, pan duan yong hu shu ru de mi ma shi fou yu shu ju ku zhong bao cun de yong hu de mi ma xiang tong def check_pwd self, pwd: from werkzeug. security import check_password_hash return check_password_hash self. pwd, pwd 4. 2 ding yi hui yuan deng lu ri zhi shu ju mo xing hui yuan deng lu ri zhi class Userlog db. Model: __tablename__ " userlog" ding yi yong hu ri zhi biao zai shu ju ku zhong de ming cheng id db. Column db. Integer, primary_key True bian hao user_id db. Column db. Integer, db. ForeignKey" user. id" suo shu hui yuan bian hao ip db. Column db. String 100 zui jin deng lu IP di zhi addtime db. Column db. DateTime, index True, default datetime. now zui jin deng lu shi jian def __repr__ self: return " lt Userlog r gt" self. id 4. 3 ding yi biao qian shu ju mo xing dian ying biao qian class Tag db. Model: __tablename__ " tag" ding yi dian ying biao qian biao zai shu ju ku zhong de ming cheng id db. Column db. Integer, primary_key True dian ying bian hao name db. Column db. String 100, unique True biao ti addtime db. Column db. DateTime, index True, default datetime. now dian ying tian jia shi jian movies db. relationship" Movie", backref" tag" dian ying wai jian de jian zhi def __repr__ self: return " lt Tag r gt" self. name 4. 4 ding yi dian ying shu ju mo xing dian ying class Movie db. Model: __tablename__ " movie" ding yi dian ying biao zai shu ju ku zhong de ming cheng id db. Column db. Integer, primary_key True bian hao title db. Column db. String 255, unique True biao ti url db. Column db. String 255, unique True di zhi info db. Column db. Text jian jie logo db. Column db. String 255, unique True feng mian star db. Column db. SmallInteger xing ji playnum db. Column db. BigInteger bo fang liang commentnum db. Column db. BigInteger ping lun liang tag_id db. Column db. Integer, db. ForeignKey" tag. id" suo shu biao qian area db. Column db. String 255 shang ying di qu release_time db. Column db. Date shang ying shi jian length db. Column db. String 100 bo fang shi jian addtime db. Column db. DateTime, index True, default datetime. now tian jia shi jian comments db. relationship" Comment", backref" movie" ping lun wai jian guan xi guan lian moviecols db. relationship" Moviecol", backref" movie" shou cang wai jian guan xi guan lian def __repr__ self: return " lt Movie r gt" self. title 4. 5 ding yi shang ying yu gao shu ju mo xing shang ying yu gao class Preview db. Model: __tablename__ " preview" ding yi dian ying shang ying yu gao biao zai shu ju ku zhong de ming cheng id db. Column db. Integer, primary_key True bian hao title db. Column db. String 255, unique True biao ti logo db. Column db. String 255, unique True feng mian addtime db. Column db. DateTime, index True, default datetime. now tian jia shi jian def __repr__ self: return " lt Preview r gt" self. title 4. 6 ding yi dian ying ping lun shu ju mo xing dian ying ping lun class Comment db. Model: __tablename__ " comment" ding yi dian ying ping lun biao zai shu ju ku zhong de ming cheng id db. Column db. Integer, primary_key True bian hao content db. Column db. Text ping lun nei rong movie_id db. Column db. Integer, db. ForeignKey" movie. id" suo shu dian ying user_id db. Column db. Integer, db. ForeignKey" user. id" suo shu yong hu addtime db. Column db. DateTime, index True, default datetime. now tian jia shi jian def __repr__ self: return " lt Comment r gt" self. id 4. 7 ding yi dian ying shou cang shu ju mo xing dian ying shou cang class Moviecol db. Model: __tablename__ " moviecol" ding yi dian ying shou cang biao zai shu ju ku zhong de ming cheng id db. Column db. Integer, primary_key True bian hao movie_id db. Column db. Integer, db. ForeignKey" movie. id" dian ying bian hao user_id db. Column db. Integer, db. ForeignKey" user. id" yong hu bian hao addtime db. Column db. DateTime, index True, default datetime tian jia shou cang shi jian def __repr__ self: return " lt Moviecol r gt" self. id 4. 8 ding yi quan xian shu ju mo xing quan xian class Auth db. Model: __tablename__ " auth" ding yi yong hu quan xian biao zai shu ju ku zhong de ming cheng id db. Column db. Integer, primary_key True bian hao name db. Column db. String 100, unique True quan xian ming cheng url db. Column db. String 100, unique True di zhi addtime db. Column db. DateTime, index True, default datetime. now tian jia shi jian def __repr__ self: return " lt Auth r gt" self. name 4. 9 ding yi jue se shu ju mo xing jue se class Role db. Model: __tablename__ " role" ding yi yong hu jue se biao zai shu ju ku zhong de ming cheng id db. Column db. Integer, primary_key True bian hao name db. Column db. String 128, unique True jue se ming cheng auths db. Column db. String 512 addtime db. Column db. DateTime, index True, default datetime. now tian jia shi jian admins db. relationship" Admin", backref" role" guan li yuan wai jian guan xi guan lian def __repr__ self: return " lt Role r gt" self. name 4. 10 ding yi guan li yuan shu ju mo xing guan li yuan class Admin db. Model: __tablename__ " admin" ding yi guan li yuan biao zai shu ju ku zhong de ming cheng id db. Column db. Integer, primary_key True bian hao name db. Column db. String 100, unique True guan li yuan zhang hao pwd db. Column db. String 100 guan li yuan mi ma is_super db. Column db. SmallInteger shi fou wei chao ji guan li yuan, wei chao ji guan li yuan role_id db. Column db. Integer, db. ForeignKey" role. id" suo shu jue se addtime db. Column db. DateTime, index True, default datetime. now adminlogs db. relationship" Adminlog", backref" admin" guan li yuan deng lu ri zhi wai jian guan xi guan lian oplogs db. relationship" Oplog", backref" admin" guan li yuan cao zuo ri zhi wai jian guan xi guan lian def __repr__ self: return " lt Admin r gt" self. name def check_pwd self, pwd: from werkzeug. security import check_password_hash return check_password_hash self. pwd, pwd 4. 11 ding yi guan li yuan deng lu ri zhi shu ju mo xing guan li yuan deng lu ri zhi class Adminlog db. Model: __tablename__ " adminlog" ding yi guan li yuan ri zhi biao zai shu ju ku zhong de ming cheng id db. Column db. Integer, primary_key True bian hao admin_id db. Column db. Integer, db. ForeignKey" admin. id" suo shu guan li yuan ip db. Column db. String 100 deng lu IP addtime db. Column db. DateTime, index True, default datetime. now deng lu shi jian def __repr__ self: return " lt Adminlog r gt" self. id 4. 12 ding yi yong hu cao zuo ri zhi cao zuo ri zhi class Oplog db. Model: __tablename__ " oplog" ding yi cao zuo ri zhi biao zai shu ju ku zhong de ming cheng id db. Column db. Integer, primary_key True bian hao admin_id db. Column db. Integer, db. ForeignKey" admin. id" suo shu guan li yuan ip db. Column db. String 100 deng lu IP reason db. Column db. String 600 cao zuo yuan yin addtime db. Column db. DateTime, index True, default datetime. now deng lu shi jian def __repr__ self: return " lt Oplog r gt" self. id zai models. py wen jian bao cun suo you de mo xing lei

5. 创建数据库

models.py文件中,定义main函数,在MariaDB数据库中生成前面定义的模型表

if __name__ == "__main__":
    db.create_all()

单独执行models.py文件,在MariaDB数据库的movie数据库中创建数据表

数据库创建完成,进入MariaDB数据库提示符,查看创建的数据表

当前文章:http://www.dribbib.com/gyr0qwx8/124280-454756-47045.html

发布时间:16:18:50

767.cc??09655救世网??开码历史记录??九龙论垣高手82344??jx017极限码皇百度??www.405068.com??金多彩84384六盒神童??www.13090.com??金虎堂??网上买码网址??

顶一下
(0)
0%
踩一下
(0)
0%
------分隔线----------------------------