from flask import Flask, render_template, request, redirect from flask_sqlalchemy import SQLAlchemy app = Flask(__name__) app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://root:zuohao1217@127.0.0.1:3306/test3' db = SQLAlchemy(app) class Importation(db.Model): data = db.Column(db.String(255), primary_key=True, autoincrement=True) province = db.Column(db.String(255)) add_input = db.Column(db.Integer) new_confirmed = db.Column(db.Integer) total_confirmed = db.Column(db.Integer) total_recovery = db.Column(db.Integer) @app.route("/select_all") def select_all(): importationList = Importation.query.order_by(Importation.data.asc()).all() #拿到数据库数据 return render_template("index.html", importation_list = importationList) @app.route("/") def index(): return select_all() #跳转提交页面 @app.route("/insert_page") def insert_page(): return render_template("insert.html") #提交功能 @app.route("/insert",methods=['GET','POST']) def insert(): #接收用户的数据 data = request.form['data'] province = request.form['province'] add_input = request.form['add_input'] new_confirmed = request.form['new_confirmed'] total_confirmed = request.form['total_confirmed'] total_recovery = request.form['total_recovery'] importation = Importation(data=data,province=province,add_input=add_input,new_confirmed=new_confirmed,total_confirmed=total_confirmed,total_recovery=total_recovery) db.session.add(importation) db.session.commit() return redirect('/') #实现删除功能 @app.route("/delete",methods=['GET']) def delete(): data = request.args.get("data") importation = Importation.query.filiter_by(data=data).first() db.session.delete(importation) db.session.commit() return redirect('/') #修改操作 @app.route("/alter",methods=['GET','POST']) def alter(): # 可以通过请求方式来改变处理该请求的具体操作 # 比如用户访问/alter页面 如果通过GET请求则返回修改页面 如果通过POST请求则使用修改操作 if request.method == 'GET': data = request.args.get("data") province = request.args.get("province") add_input = request.args.get("add_input") new_confirmed = request.args.get("new_confirmed") total_confirmed = request.args.get("total_confirmed") total_recovery = request.args.get("total_recovery") importation = Importation(data = data,province=province,add_input=add_input,new_confirmed = new_confirmed,total_confirmed=total_confirmed,total_recovery=total_recovery) return render_template("alter.html",importation = importation) else: #接收参数,修改数据 data = request.form["data"] province = request.form['province'] add_input = request.form['add_input'] new_confirmed = request.form['new_confirmed'] total_confirmed = request.form['total_confirmed'] total_recovery = request.form['total_recovery'] importation = Importation.query.filter_by(data=data).first() importation.province = province importation.add_input = add_input importation.new_confirmed = new_confirmed importation.total_confirmed = total_confirmed importation.total_recovery = total_recovery db.session.commit() return redirect('/') if __name__ == '__main__': app.run()