flask实现增删改查CURD操作
项目结构图:
app.py
1 from flask import Flask, render_template, redirect, request, url_for 2 from flask_sqlalchemy import SQLAlchemy 3 from flask_migrate import Migrate 4 5 app = Flask(__name__) 6 # 数据库的配置信息 7 HOSTNAME = '127.0.0.1' 8 POST = '3306' 9 DATABASE = 'mytest' 10 USERNAME = 'root' 11 PASSWORD = '123456' 12 DB_URI = 'mysql+pymysql://{}:{}@{}:{}/{}'.format(USERNAME, PASSWORD, 13 HOSTNAME, POST, DATABASE) 14 app.config['SQLALCHEMY_DATABASE_URI'] = DB_URI 15 app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True 16 db = SQLAlchemy(app) 17 migrate = Migrate(app, db) 18 ''' 19 使用flask_migrate把数据模型映射到MySql数据中 20 打开Pycharm的Terminal输入命令:flask db init->flask db migrate->flask db upgrade 21 ''' 22 23 24 class Phone(db.Model): 25 __tablename__ = 'phone' 26 id = db.Column(db.Integer, primary_key=True, autoincrement=True) 27 phonename = db.Column(db.String(20), nullable=False) 28 price = db.Column(db.Integer, nullable=False) 29 30 31 class Person(db.Model): 32 __tablename__ = 'person' 33 id = db.Column(db.Integer, primary_key=True, autoincrement=True) 34 name = db.Column(db.String(20), nullable=False) 35 age = db.Column(db.Integer, nullable=False) 36 pid = db.Column(db.Integer, db.ForeignKey('phone.id')) 37 phone = db.relationship('Phone', backref='person') 38 39 40 # 添加数据 41 @app.route('/add', methods=['get', 'post']) 42 def add(): 43 personname = request.form.get('personname') 44 age = request.form.get('age') 45 phonename = request.form.get('phonename') 46 price = request.form.get('price') 47 person = Person(name=personname, age=age) 48 phone = Phone(phonename=phonename, price=price) 49 person.phone = phone 50 db.session.add(person) 51 db.session.add(phone) 52 db.session.commit() 53 return redirect('/') 54 55 56 # 查询数据 57 @app.route('/search', methods=['get', 'post']) 58 def search(): 59 persons = Person.query.all() 60 return render_template('search.html', person=persons) 61 # 根据id获取一条,需要修改的数据 62 @app.route('/get_id/<pid>', methods=['get', 'post']) 63 def get_id(pid): 64 pe = Person.query.filter_by(id=pid)[0] 65 getId = request.form.get('btn') 66 return render_template('query_id.html', pe=pe) 67 68 69 ''' 70 修改数据 71 ''' 72 @app.route('/upgrade/<int:peid>', methods=['get', 'post']) 73 def upgrade(peid): 74 pname = request.form.get('pname') 75 page = request.form.get('page') 76 pphone = request.form.get('pphone') 77 pprice = request.form.get('pprice') 78 person = Person.query.filter_by(id=peid)[0] 79 person.name = pname 80 person.age = page 81 person.phone.phonename = pphone 82 person.phone.price = pprice 83 db.session.commit() 84 return redirect(url_for('search')) 85 ''' 86 根据id删除数据 87 ''' 88 @app.route('/delete/<int:did>', methods=['get', 'post']) 89 def delete(did): 90 Person.query.filter_by(id=did).delete() 91 db.session.commit() 92 return redirect(url_for('search')) 93 @app.route('/') 94 def index(): 95 return render_template('add.html') 96 if __name__ == '__main__': 97 app.run()
父模板页面代码base.html
1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <link rel="stylesheet" href="{{ url_for('static',filename='bootstrap/css/bootstrap.css') }}"> 6 <script src="{{ url_for('static',filename='bootstrap/js/jquery-3.5.1.js') }}"></script> 7 <script src="{{ url_for('static',filename='bootstrap/js/bootstrap.js') }}"></script> 8 <title>{% block title %}{% endblock %}</title> 9 {% block head %}{% endblock %} 10 </head> 11 <body> 12 <div class="container"> 13 {% block body %}{% endblock %} 14 </div> 15 </body> 16 </html>
添加数据的模板页面add.html
1 {% extends 'base.html' %} 2 {% block title %}增加数据{% endblock %} 3 {% block head %}{% endblock %} 4 {% block body %} 5 <div class="row"> 6 <div class="col"></div> 7 <div class="col-5" style="margin-top: 30px"> 8 <form action="{{ url_for('add') }}" method="post"> 9 <div class="form-group row"> 10 <label class="col-sm-2 col-form-label">姓名:</label> 11 <div class="col-sm-10"> 12 <input type="text" name="personname" class="form-control"> 13 </div> 14 </div> 15 <div class="form-group row"> 16 <label class="col-sm-2 col-form-label">年龄:</label> 17 <div class="col-sm-10"> 18 <input type="text" name="age" class="form-control"> 19 </div> 20 </div> 21 <div class="form-group row"> 22 <label class="col-sm-2 col-form-label">手机:</label> 23 <div class="col-sm-10"> 24 <input type="text" name="phonename" class="form-control"> 25 </div> 26 </div> 27 <div class="form-group row"> 28 <label class="col-sm-2 col-form-label">价格:</label> 29 <div class="col-sm-10"> 30 <input type="text" name="price" class="form-control"> 31 </div> 32 </div> 33 <div class="form-group " style="float: left;text-align: right"> 34 <button type="submit" class="btn btn-primary">添加</button> 35 </div> 36 </form> 37 <div class="form-group " style="text-align: right"> 38 <form action="{{ url_for('search') }}" method="post"> 39 <button type="submit" class="btn btn-primary">查询</button> 40 </form> 41 </div> 42 </div> 43 <div class="col"></div> 44 </div> 45 {% endblock %}
添加数据模板的页面效果图:
查询数据的模板页面代码search.html
1 {% extends 'base.html' %} 2 {% block title %}查询数据{% endblock %} 3 {% block head %}{% endblock %} 4 {% block body %} 5 <table class="table table-bordered table-hover" 6 style="text-align: center;margin-top: 30px"> 7 <thead> 8 <tr> 9 <th scope="col">编号</th> 10 <th scope="col">姓名</th> 11 <th scope="col">年龄</th> 12 <th scope="col">手机</th> 13 <th scope="col">价格</th> 14 <th scope="col" colspan="2">操作</th> 15 </tr> 16 </thead> 17 <tbody> 18 {% for item in person %} 19 <tr> 20 <th scope="row">{{ item.id }}</th> 21 <td>{{ item.name }}</td> 22 <td>{{ item.age }}</td> 23 <td>{{ item.phone.phonename }}</td> 24 <td>{{ item.phone.price }}</td> 25 <td> 26 <form action="{{ url_for('get_id',pid=item.id) }}" method="post"> 27 <button type="submit" class="btn btn-primary">修改</button> 28 </form> 29 </td> 30 <td> 31 <form action="{{ url_for('delete',did=item.id) }}" method="post"> 32 <button type="submit" class="btn btn-danger btn-sm">删除</button> 33 </form> 34 </td> 35 </tr> 36 {% endfor %} 37 </tbody> 38 </table> 39 {% endblock %}
查询数据的模板效果图:
根据id查询需要修改的数据模板页面代码query_id.html
{% extends 'base.html' %} {% block title %}修改数据{% endblock %} {% block head %}{% endblock %} {% block body %} <div class="row"> <div class="col"></div> <div class="col-5" style="margin-top: 30px"> <form action="{{ url_for('upgrade',peid=pe.id) }}" method="post"> <div class="form-group row"> <label class="col-sm-2 col-form-label">姓名:</label> <div class="col-sm-10"> <input type="text" name="pname" value="{{ pe.name }}" class="form-control"> </div> </div> <div class="form-group row"> <label class="col-sm-2 col-form-label">年龄:</label> <div class="col-sm-10"> <input type="text" name="page" value="{{ pe.age }}" class="form-control"> </div> </div> <div class="form-group row"> <label class="col-sm-2 col-form-label">手机:</label> <div class="col-sm-10"> <input type="text" name="pphone" value="{{ pe.phone.phonename }}" class="form-control"> </div> </div> <div class="form-group row"> <label class="col-sm-2 col-form-label">价格:</label> <div class="col-sm-10"> <input type="text" name="pprice" value="{{ pe.phone.price }}" class="form-control"> </div> </div> <div class="form-group " style="text-align: right"> <button type="submit" class="btn btn-primary">修改</button> </div> </form> </div> <div class="col"></div> </div> {% endblock %}
模板的页面效果图:
数据库的表结构: