from flask import Flask,render_template,request,redirect from flask_sqlalchemy import SQLAlchemy import config app = Flask(__name__) app.config.from_object(config) db=SQLAlchemy(app) class book(db.Model): __tablename__ = 'book' id = db.Column(db.Integer,primary_key=True,nullable=True) name = db.Column(db.String(100),nullable=True) author = db.Column(db.String(200),nullable=True) publish = db.Column(db.String(20),nullable=True) price = db.Column(db.String(20),nullable=True) types = db.Column(db.String(100),nullable=True) pages = db.Column(db.Integer, nullable=True) # 图书管理系统 @app.route('/') def login(): return render_template('login.html') # 图书管理系统主页面 @app.route('/index/') def index(): b = book.query.all() return render_template('index.html',b = b) # 新增页面 @app.route('/addi/',methods = ["GET","POST"]) def addi(): if request.method == 'GET': return render_template('addi.html') else: name = request.form.get('name') author = request.form.get('author') publish = request.form.get('publish') price = request.form.get('price') types = request.form.get('types') pages = request.form.get('pages') bb = book(name = name,author = author,publish = publish,price = price,types = types,pages = pages) db.session.add(bb) db.session.commit() return redirect('/index/') # 删除页面 @app.route('/delete/<id>') def delete(id): b = book.query.filter(book.id == id).first() if b: db.session.delete(b) db.session.commit() return redirect('/index/') if __name__ == '__main__': db.drop_all() db.create_all() # 添加表数据 b1 = book(name = '互联网思维',author = '赵大伟',publish = '2014-03-20',price = '49',typ es = 'it',pages= 280) b2 = book(name = '互联网:从it到dt', author='阿里研究院', publish='2015-05-30', price='59', types='it', pages=285) b3 = book(name = '道德经', author='老子', publish='1955-01-14', price='88', types='国学', pages=81) db.session.add_all([b1,b2,b3]) db.session.commit() app.run(debug=True)