-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
72 lines (60 loc) · 2.12 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
from flask import Flask,render_template
from flask_sqlalchemy import SQLAlchemy
from flask_wtf import FlaskForm
from wtforms import StringField,SubmitField
from wtforms.validators import DataRequired
app = Flask(__name__)
app.secret_key='dry'
#1.数据库配置
app.config['SQLALCHEMY_DATABASE_URI']="mysql+pymysql://root:[email protected]:3306/flask_books"
app.config['SQLALCHEMY_TRACK_MODIFICATIONS']=False
db=SQLAlchemy(app)
#2.定义作者类和书本类
class Author(db.Model):
query = None
__tablename__ = 'authors'
id=db.Column(db.Integer,primary_key=True)
name = db.Column(db.String(16), unique=True)
books=db.relationship('Book',backref='author')
def __repr__(self):
return 'Author:%s'%self.name
class Book(db.Model):
__tablename__='books'
id=db.Column(db.Integer,primary_key=True)
name=db.Column(db.String(16),unique=True)
author_id=db.Column(db.Integer,db.ForeignKey('authors.id'))
def __repr__(self):
return 'Book:%s %s'%(self.name,self.author_id)
#5.使用WTF显示表单
#自定义表单类
class AuthorForm(FlaskForm):
author=StringField('作者',validators=[DataRequired()])
book=StringField('书名',validators=[DataRequired()])
submit=SubmitField('提交')
@app.route('/',methods=['GET','POST'])
def hello_world():
#4.使用模板显示这些信息
#查询所有数据,将数据传给模板
authors=Author.query.filter_by(name='zs').first()#,authors=authors
flask_form=AuthorForm()
return render_template('books.html',authors=authors,form=flask_form)
#3.添加数据
db.drop_all()
db.create_all()
#生成数据
au1 = Author(name='zs')
au2 = Author( name='ls')
au3 = Author(name='ww')#把数据提交给用户会话*
db.session.add_all( [au1, au2, au3])#提交会话
db.session.commit()
bk1 = Book(name='zxc1',author_id=au1.id)
bk2 = Book(name="zxc2",author_id=au1.id)
bk3 = Book(name="zxc3",author_id=au2.id)
bk4 = Book(name="zxc4",author_id=au3.id)
bk5 = Book(name="zxc5",author_id=au3.id)
#把数据提交给用户会话
db.session.add_all( [bk1,bk2,bk3,bk4,bk5])
#提交会话
db.session.commit()
if __name__ == '__main__':
app.run(debug=True)