简单的flask ‘s Demo

| |
| from flask import Flask, render_template, jsonify, request, redirect, url_for, session |
| import functools |
| |
| app = Flask(__name__) |
| app.secret_key = 'adsa12dsa3456dasdsadsa' |
| |
| DATA_DICT = { |
| 1: {"name": "陈三", "age": 73}, |
| 2: {"name": "程思", "age": 84} |
| } |
| |
| |
| def auth(func): |
| @functools.wraps(func) |
| def inner(*args, **kwargs): |
| username = session.get('xxx') |
| if not username: |
| redirect('/login') |
| |
| return inner() |
| |
| |
| @app.route('/login', methods=['GET', 'POST']) |
| def login(): |
| |
| |
| if request.method == 'GET': |
| return render_template("login.html") |
| |
| user = request.form.get('user') |
| pwd = request.form.get('pwd') |
| if user == 'root' and pwd == '123456': |
| session['xxx'] = user |
| return redirect('/index') |
| |
| error = '用户名或密码错误' |
| |
| return render_template("login.html", error=error) |
| |
| |
| @app.route('/index', endpoint='idx') |
| @auth |
| def index(): |
| |
| data_dict = DATA_DICT |
| return render_template('index.html', data_dict=data_dict) |
| |
| |
| @app.route('/edit', methods=['GET', 'POST']) |
| @auth |
| def edit(): |
| nid = request.args.get('nid') |
| nid = int(nid) |
| if request.method == 'GET': |
| info = DATA_DICT[nid] |
| |
| |
| return render_template('edit.html', info=info) |
| user = request.form.get('user') |
| age = request.form.get('age') |
| DATA_DICT[nid]['name'] = user |
| DATA_DICT[nid]['age'] = age |
| return redirect(url_for('idx')) |
| |
| |
| @app.route('/delete/<int:nid>') |
| @auth |
| def delete(nid): |
| |
| del DATA_DICT[nid] |
| |
| |
| return redirect(url_for('idx')) |
| |
| |
| if __name__ == '__main__': |
| app.run(debug=True) |
| |
| // login.html |
| <!DOCTYPE html> |
| <html lang="en"> |
| <head> |
| <meta charset="UTF-8"> |
| <title>Title</title> |
| </head> |
| <body> |
| <h1>登录页面</h1> |
| <form action="" method="post"> |
| <input type="text" name="user"> |
| <input type="text" name="pwd"> |
| <input type="submit" value="提交"> <span style="color: red" >{{ error }}</span> |
| </form> |
| </body> |
| </html> |
| // index.html |
| <!DOCTYPE html> |
| <html lang="en"> |
| <head> |
| <meta charset="UTF-8"> |
| <title>Title</title> |
| </head> |
| <body> |
| <h1>用户列表</h1> |
| <table border="1"> |
| <thead> |
| <tr> |
| <th>ID</th> |
| <th>用户名</th> |
| <th>年龄</th> |
| <th>操作</th> |
| </tr> |
| </thead> |
| <tbody> |
| {% for key, value in data_dict.items() %} |
| <tr> |
| <td>{{key}}</td> |
| <td>{{value['name']}}</td> |
| <td>{{value['age']}}</td> |
| <td> |
| <a href="/edit?nid={{ key }}">编辑</a> |
| <a href="/delete/{{ key }}">删除</a> |
| </td> |
| </tr> |
| {% endfor %} |
| </tbody> |
| </table> |
| </body> |
| </html> |
| // edit.html |
| <!DOCTYPE html> |
| <html lang="en"> |
| <head> |
| <meta charset="UTF-8"> |
| <title>Title</title> |
| </head> |
| <body> |
| <h1>修改</h1> |
| <form action="" method="post"> |
| <input type="text" name="user" value="{{ info.name }}"> |
| <input type="text" name="age" value="{{ info.age }}"> |
| <input type="submit" value="提交"> |
| </form> |
| </body> |
| </html> |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步