flask實現簡單數據的增刪改查(藍圖)


main文件為主文件,實現藍圖的引入與注冊

from flask import Flask from add_Flask import add from del_Flask import dele from select_Flask import select from update_Flask import update def flask_app(): app=Flask(__name__) app.register_blueprint(add.add) #對藍圖進行注冊
 app.register_blueprint(dele.dele) app.register_blueprint(select.select) app.register_blueprint(update.update) return app #以函數方式返回app對象
 flask_app().run(debug=True)

data文件存放列表數據:

STUDENT = [ {'id': 1, 'name': '王小璐', 'age': 18, 'gender': ''}, {'id': 2, 'name': '李曉芬', 'age': 21, 'gender': ''}, {'id': 3, 'name': '陳志鵬', 'age': 24, 'gender': ''}, {'id': 4, 'name': '胡歌', 'age': 35, 'gender': ''}, {'id': 5, 'name': '趙安琪', 'age': 28, 'gender': ''}, ]

添加數據藍圖:

from flask import Blueprint,request,redirect,render_template from blueview.flask_Data import STUDENT add=Blueprint('add',__name__,template_folder="temp",static_folder='static') @add.route('/add',methods=['GET','POST']) def add_view(): if request.method=='POST': stu_dic={ 'id':request.form.get('id'), 'name':request.form.get('name'), 'age':request.form.get('age'), 'gender':request.form.get('gender') } STUDENT.append(stu_dic) return redirect('/select') return render_template('add.html')
add藍圖
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <link rel="stylesheet" href="/static/bootstrap/css/bootstrap.min.css">
    <title>添加學生</title>
</head>
<body>
<script src="/static/jquery-3.3.1.min.js"></script>
<script src="/static/bootstrap/js/bootstrap.min.js"></script>

<form method="post" class="form-horizontal" style="margin-top: 50px">
    <div class="form-group">
        <label for="inputID" class="col-sm-2 control-label">編號:</label>
        <div class="col-sm-4">
            <input type="text" class="form-control" name="id" placeholder="">
        </div>
    </div>
    <div class="form-group">
        <label for="inputNAME" class="col-sm-2 control-label">姓名:</label>
        <div class="col-sm-4">
            <input type="text" class="form-control" name="name" placeholder="">
        </div>
    </div>
    <div class="form-group">
        <label for="inputAGE" class="col-sm-2 control-label">年齡:</label>
        <div class="col-sm-4">
            <input type="text" class="form-control" name="age" placeholder="">
        </div>
    </div>
    <div class="form-group">
        <label for="inputGENDER" class="col-sm-2 control-label">性別:</label>
        <div class="col-sm-4">
            <input type="text" class="form-control" name="gender" placeholder="">
        </div>
    </div>

    <div class="form-group">
        <label class="col-sm-2 control-label"></label>
        <div class="col-sm-4">
            <input class="btn btn-success" type="submit" value="添加學生">
        </div>
    </div>

</form>

</body>
</html>
add.html

刪除數據藍圖:

from flask import Blueprint,render_template from blueview.flask_Data import STUDENT dele=Blueprint('dele',__name__,template_folder="temp",static_folder='static') @dele.route('/dele/<int:nid>',methods=['GET','POST']) def del_view(nid): # print(nid)
    ST=[] for stu in STUDENT: if stu["id"]==nid: pass
        else: ST.append(stu) return render_template('select.html',student=ST)
dele藍圖
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <link rel="stylesheet" href="/static/bootstrap/css/bootstrap.min.css">
    <title>學生信息</title>
</head>
<body>
<script src="/static/jquery-3.3.1.min.js"></script>
<script src="/static/bootstrap/js/bootstrap.min.js"></script>

<table class="table table-bordered">
    <thead>
    <tr>
        <th>編號</th>
        <th>姓名</th>
        <th>年齡</th>
        <th>性別</th>
        <th colspan="2" rowspan="1" style='text-align: center;'>編輯</th>
    </tr>
    </thead>
    <tbody> {% for st in student %} <tr>
        <td>{{ st.id }}</td>
        <td>{{ st.name }}</td>
        <td>{{ st.age }}</td>
        <td>{{ st.gender }}</td>
        <td><a href="/update/{{ st.id }}"  class="btn btn-primary">修改</a> </td>
        <td><a href="/dele/{{ st.id }}"  class="btn btn-danger">刪除</a></td>
        </tr> {% endfor %} </tbody>

</table>
<a href="/add"  class="btn btn-success">添加學生</a>
</body>
</html>
dele.html

更新數據藍圖:

from flask import Blueprint,request,redirect,render_template from blueview.flask_Data import STUDENT update=Blueprint('update',__name__,template_folder='temp',static_folder='static') @update.route('/update/<int:nid>',methods=['GET','POST']) def update_view(nid): print(nid) if request.method=='POST': stu_dic={ 'id':request.form.get('id'), 'name': request.form.get('name'), 'age': request.form.get('age'), 'gender': request.form.get('gender') } for index,stu in enumerate(STUDENT): if stu['id']==int(stu_dic['id']): STUDENT[index]=stu_dic return redirect('/select') for stu in STUDENT: if stu['id']==int(nid): return render_template('update.html',student=stu) return render_template("update.html", student="")
update藍圖
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>學生列表</title>
</head>
<body>
<form method="post">
    <input type="text" name="id" hidden value="{{ student.id }}"><br> 姓名:<input type="text" name="name" value="{{ student.name }}"><br> 年齡:<input type="text" name="age" value="{{ student.age }}"><br> 性別:<input type="text" name="gender" value="{{ student.gender }}"><br>
    <input type="submit" value="修改信息">
</form>

</body>
</html>
update.html

查詢數據藍圖:

from flask import Blueprint,render_template from blueview.flask_Data import STUDENT select=Blueprint('select',__name__,template_folder="temp",static_folder="static") @select.route('/select') def select_view(): return render_template('select.html',student=STUDENT)
select藍圖
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <link rel="stylesheet" href="/static/bootstrap/css/bootstrap.min.css">
    <title>學生信息</title>
</head>
<body>
<script src="/static/jquery-3.3.1.min.js"></script>
<script src="/static/bootstrap/js/bootstrap.min.js"></script>

<table class="table table-bordered">
    <thead>
    <tr>
        <th>編號</th>
        <th>姓名</th>
        <th>年齡</th>
        <th>性別</th>
        <th colspan="2" rowspan="1" style='text-align: center;'>編輯</th>
    </tr>
    </thead>
    <tbody> {% for st in student %} <tr>
        <td>{{ st.id }}</td>
        <td>{{ st.name }}</td>
        <td>{{ st.age }}</td>
        <td>{{ st.gender }}</td>
        <td><a href="/update/{{ st.id }}"  class="btn btn-primary">修改</a> </td>
        <td><a href="/dele/{{ st.id }}"  class="btn btn-danger">刪除</a></td>
        </tr> {% endfor %} </tbody>

</table>
<a href="/add"  class="btn btn-success">添加學生</a>
</body>
</html>
select.html

項目的完整文件結構為:

其中static為靜態資源文件夾

實現的效果圖如下:


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM