flask案例

简单的flask ‘s Demo

#app.py
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():  # put application's code here
    # return jsonify({'code': 100, 'data': [1, 2, 3]})
    # return 'Hello World!'  # HttpRespose
    if request.method == 'GET':
        return render_template("login.html")
    # print(request.form)
    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')  # endpoint不写默认函数名 (不能重名)
@auth
def index():
    # return '首页'
    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]
        # print(nid)
        # return '修改'
        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):
    # print(nid)
    del DATA_DICT[nid]
    # return '删除'
    # return redirect('/index')
    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>
posted @ 2023-04-18 23:06  __username  阅读(32)  评论(0)    收藏  举报