You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
83 lines
2.2 KiB
83 lines
2.2 KiB
# -*- encoding: utf-8 -*-
|
|
'''
|
|
@File : setup.py
|
|
@License : (C)Copyright 2018-2022
|
|
|
|
@Modify Time @Author @Version @Desciption
|
|
------------ ------- -------- -----------
|
|
2023/4/18 10:18 zart20 1.0 None
|
|
'''
|
|
|
|
|
|
from flask import Flask,render_template,request,redirect
|
|
from flask_sqlalchemy import SQLAlchemy
|
|
|
|
app = Flask(__name__)
|
|
|
|
|
|
#设置数据库连接
|
|
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://root:123123@127.0.0.1:3306/example'
|
|
|
|
with app.app_context():
|
|
db = SQLAlchemy(app)
|
|
|
|
# 定义城市模型
|
|
class City(db.Model):
|
|
id = db.Column(db.Integer,primary_key=True,autoincrement=True)
|
|
provincename = db.Column(db.String(255))
|
|
cityname = db.Column(db.String(255))
|
|
usernumber = db.Column(db.Integer)
|
|
|
|
def __repr__(self):
|
|
return f"<{self.cityname!r}>"
|
|
|
|
class User(db.Model):
|
|
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
|
|
name = db.Column(db.String(120))
|
|
|
|
# 查询所有数据
|
|
@app.route("/select")
|
|
def select_all():
|
|
city_list = City.query.order_by(City.id.desc()).all()
|
|
#将数据给到前端页面
|
|
return render_template("index.html",city_list = city_list)
|
|
|
|
|
|
# 首页
|
|
@app.route("/")
|
|
def index():
|
|
return select_all()
|
|
|
|
|
|
@app.route("/insert_page")
|
|
def insert_page():
|
|
#跳转至添加信息页面
|
|
return render_template("insert.html")
|
|
|
|
@app.route("/delete/<c_id>")
|
|
def delete_url(c_id):
|
|
c_id = int(c_id) # 获取ID
|
|
city = City.query.get(c_id) # 查找数据对象
|
|
db.session.delete(city) # 删除对象
|
|
db.session.commit() # 提交修改
|
|
return redirect('/')
|
|
|
|
|
|
@app.route("/insert",methods=['GET','POST'])
|
|
def insert():
|
|
if request.method == 'POST':
|
|
provincename = request.form.get('province')
|
|
cityname = request.form['city']
|
|
usernumber = request.form['usernumber']
|
|
city = City(provincename=provincename,cityname=cityname,usernumber=usernumber)
|
|
print(city)
|
|
db.session.add(city)
|
|
db.session.commit()
|
|
return redirect('/') # 重定向到首页
|
|
|
|
if __name__ == '__main__':
|
|
with app.app_context(): # 将db.create_all()添加到app上下文来执行
|
|
db.create_all()
|
|
|
|
|
|
app.run(host="0.0.0.0", port=8080, debug=True) |