From bfca4f146a001e9a620c52d9c7c630bb2c034870 Mon Sep 17 00:00:00 2001 From: zart2007 Date: Tue, 18 Apr 2023 13:42:06 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app.py | 2 +- setup.py | 74 +++++++++++++++++++++++++++++++++++++++++++ templates/index.html | 32 ++++++++++++++----- templates/index1.html | 16 ++++++++++ templates/insert.html | 17 ++++++++++ test_py_orm.py | 58 +++++++++++++++++++++++++++++++++ 6 files changed, 190 insertions(+), 9 deletions(-) create mode 100644 setup.py create mode 100644 templates/index1.html create mode 100644 templates/insert.html create mode 100644 test_py_orm.py diff --git a/app.py b/app.py index ed4934f..b015e9d 100644 --- a/app.py +++ b/app.py @@ -22,7 +22,7 @@ def hello(): @app.route('/hello/') @app.route('/hello//') def hello_2(name=None): - return render_template('index.html', name=name) + return render_template('index1.html', name=name) @app.route("/99/") diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..22ee2bc --- /dev/null +++ b/setup.py @@ -0,0 +1,74 @@ +# -*- 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("/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) \ No newline at end of file diff --git a/templates/index.html b/templates/index.html index 26718e9..c279117 100644 --- a/templates/index.html +++ b/templates/index.html @@ -1,16 +1,32 @@ - - Hello from Flask - + Title - {% if name %} -

Hello {{ name }}

- {% else %} -

Hello World!

- {% endif %} +
+

城市信息

+ 添加城市信息 + + + + + + + + {% for item in city_list %} + + + + + + + {% endfor %} + +
provincecitynameusernumber操作
{{ item.provincename }}{{ item.cityname }}{{ item.usernumber }}编辑,删除
+
+ + \ No newline at end of file diff --git a/templates/index1.html b/templates/index1.html new file mode 100644 index 0000000..26718e9 --- /dev/null +++ b/templates/index1.html @@ -0,0 +1,16 @@ + + + + + + Hello from Flask + + + + {% if name %} +

Hello {{ name }}

+ {% else %} +

Hello World!

+ {% endif %} + + \ No newline at end of file diff --git a/templates/insert.html b/templates/insert.html new file mode 100644 index 0000000..904889a --- /dev/null +++ b/templates/insert.html @@ -0,0 +1,17 @@ + + + + + Title + + + +

添加信息

+
+ 省份:
+ 城市名称:
+ 用户数:
+ +
+ + \ No newline at end of file diff --git a/test_py_orm.py b/test_py_orm.py new file mode 100644 index 0000000..af0452b --- /dev/null +++ b/test_py_orm.py @@ -0,0 +1,58 @@ +# -*- encoding: utf-8 -*- +''' +@File : test_py_orm.py +@License : (C)Copyright 2018-2022 + +@Modify Time @Author @Version @Desciption +------------ ------- -------- ----------- +2023/4/18 10:14 zart20 1.0 None +''' + +from flask import Flask +from flask_sqlalchemy import SQLAlchemy + + +app = Flask(__name__) + +#设置连接数据库的URL +app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://root:123123@127.0.0.1:3306/example' + +#设置每次请求结束后会自动提交数据库中的改动 +app.config['SQLALCHEMY_COMMIT_ON_TEARDOWN'] = True + +app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True +#查询时会显示原始SQL语句 +app.config['SQLALCHEMY_ECHO'] = True +db = SQLAlchemy(app) + +class Role(db.Model): + # 定义表名 + __tablename__ = 'roles' + # 定义列对象 + id = db.Column(db.Integer, primary_key=True) + name = db.Column(db.String(64), unique=True) + us = db.relationship('User', backref='role') + + +class User(db.Model): + __tablename__ = 'users' + id = db.Column(db.Integer, primary_key=True) + name = db.Column(db.String(64), unique=True, index=True) + email = db.Column(db.String(64),unique=True) + pswd = db.Column(db.String(64)) + role_id = db.Column(db.Integer, db.ForeignKey('roles.id')) + +if __name__ == '__main__': + db.drop_all() + db.create_all() + ro1 = Role(name='admin') + ro2 = Role(name='user') + db.session.add_all([ro1,ro2]) + db.session.commit() + us1 = User(name='wang',email='wang@163.com',pswd='123456',role_id=ro1.id) + us2 = User(name='zhang',email='zhang@189.com',pswd='201512',role_id=ro2.id) + us3 = User(name='chen',email='chen@126.com',pswd='987654',role_id=ro2.id) + us4 = User(name='zhou',email='zhou@163.com',pswd='456789',role_id=ro1.id) + db.session.add_all([us1,us2,us3,us4]) + db.session.commit() + app.run(debug=True) \ No newline at end of file