赞
踩
# models.py : 模型,数据库
'''
模型 === 数据库
类 ——> 表结构
类属性 ——> 表字段
一个对象 ——> 表的一行数据
'''
from .exts import db
# 模型Model:类
# 必须继承 db.Model User才能从普通的类变成模型
# 作者
class Author(db.Model):
__tablename__ = 'author' # 表名,如果不写表名,也会自动生成(类名小写),表名一定要小写!
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
name = db.Column(db.String(30))
age = db.Column(db.Integer, default=1)
sex = db.Column(db.Boolean, default=True) # True表示男
email = db.Column(db.String(200))
# 关系:Author可以调用books,Book可以反向调用my_auther
books = db.relationship('Book', backref='my_auther', lazy='dynamic')
# 中间表(书籍-出版社)
book_publish = db.Table(
'book_publisher',
db.Column('book_id', db.Integer, db.ForeignKey('book.id'), primary_key=True),
db.Column('publisher_id', db.Integer, db.ForeignKey('publisher.id'), primary_key=True)
)
# 书籍
class Book(db.Model):
__tablename__ = 'book' # 表名
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
title = db.Column(db.String(100), unique=True)
date = db.Column(db.DateTime) # 书籍日期
# 1对多:外键
author_id = db.Column(db.Integer, db.ForeignKey(Author.id))
# 出版社
class Publisher(db.Model):
__tablename__ = 'publisher' # 表名
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
name = db.Column(db.String(30), unique=True)
address = db.Column(db.String(200))
city = db.Column(db.String(100))
province = db.Column(db.String(100))
country = db.Column(db.String(100))
website = db.Column(db.String(100))
# 多对多,关联book表,secondary是设置中间表
books = db.relationship('Book', backref='publishers', secondary=book_publish, lazy='dynamic')
总结:添加数据可以在pycharm专业版自带的数据库中操作如上,也可以在workbench中操作(一种界面操作,一种SQL语句操作)。
app
from App import creat_app
app = creat_app()
if __name__ == '__main__':
app.run(debug=True)
__init __
# __init__.py : 初始化文件,创建Flask应用
from flask import Flask
from .views import blue
from .exts import init_exts
def creat_app():
app = Flask(__name__)
# 注册蓝图
app.register_blueprint(blueprint=blue)
# 配置数据库
# db_uri = 'sqlite:///sqlite3.db'
db_uri = 'mysql+pymysql://root:123456@localhost:3306/bookdb' # mysql的配置
app.config['SQLALCHEMY_DATABASE_URI'] = db_uri
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False # 禁止对象追踪修改
# 初始化插件
init_exts(app=app)
return app
exts
from flask_sqlalchemy import SQLAlchemy # ORM(对象关系映射)
from flask_migrate import Migrate # 数据迁移
db = SQLAlchemy()
migrate = Migrate()
def init_exts(app):
db.init_app(app=app)
migrate.init_app(app=app,db=db)
views
# 在views.py中放路由和视图函数
from flask import Blueprint, request, render_template
from .models import * #后面是用views来控制数据库的,所以要在views中导入models文件
# 蓝图
blue = Blueprint('book', __name__)
@blue.route('/')
@blue.route('/bookindex/')
def book_index():
return render_template('book_index.html')
@blue.route('/booklist/')
def book_list():
books = Book.query.all()
return render_template('book_list.html', books=books)
@blue.route('/bookdetail/<int:bid>/') # 路由传参
def book_detail(bid):
book = Book.query.get(bid)
return render_template('book_detail.html', book=book)
# 作者详情
@blue.route('/authordetail/<int:aid>/') # 路由传参
def author_detail(aid):
author = Author.query.get(aid)
return render_template('author_detail.html', author=author)
# 出版社详情
@blue.route('/publisherdetail/<int:pid>/') # 路由传参
def publisher_detail(pid):
publisher = Publisher.query.get(pid)
return render_template('publisher_detail.html', publisher=publisher)
author_detail.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>作者详情</title>
</head>
<body>
<h2>作者详情</h2>
<p>作者姓名:{{ author.name }}</p>
<p>作者年龄:{{ author.age }}</p>
<p>作者性别:{{ author.sex }}</p>
<p>作者Email:{{ author.email }}</p>
<hr>
<h3>作者作品集合:</h3>
<p>
{% for book in author.books %}
<a href="/bookdetail/{{ book.id }}/">《{{ book.title }}》</a>
{% if not loop.last %}|{% endif %}
{% endfor %}
</p>
</body>
</html>
book_detail.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>书籍详情</title>
</head>
<body>
<h2>《{{ book.title }}》</h2>
<p>书籍出版时间:{{ book.date }}</p>
<p>作者:
<a href="/authordetail/{{ book.my_author.id }}/">{{ book.my_author.name }}</a>
</p>
<p>出版社:
{% for publisher in book.publishers %}
<a href="/publisherdetail/{{ publisher.id }}/">{{ publisher.name }}</a> |
{% endfor %}
</p>
</body>
</html>
book_index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>书籍首页</title>
</head>
<body>
<h2>书籍首页</h2>
<a href="/booklist/">查看所有书籍</a>
</body>
</html>
book_list.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>书籍列表</title>
</head>
<body>
<h2>书籍列表</h2>
<ul>
{% for book in books %}
<li>
<a href="/bookdetail/{{ book.id }}/">{{ book.title }}</a>
</li>
{% endfor %}
</ul>
</body>
</html>
publisher_detail.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>出版社详情</title>
</head>
<body>
<h2>出版社详情</h2>
<p>出版社名称:{{ publisher.name }}</p>
<p>出版社地址/城市/省份/国家:{{ publisher.address }}/{{ publisher.city }}/{{ publisher.province }}/{{ publisher.conuntry }}</p>
<p>出版社网址:{{ publisher.website }}</p>
<hr>
<p>出版社出版过的书籍:
{% for book in publisher.books %}
<a href="/bookdetail/{{ book.id }}/">《{{ book.title }}》</a>
{% if not loop.last %}|{% endif %}
{% endfor %}
</p>
</body>
</html>
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。