Flask第一个实例

Flask实现

from flask import Flask,render_template,request,redirect,session

app = Flask(__name__) # 一个Flask类的对象
app.secret_key = 'u2jksidjflsduwerjl'
app.debug = True
USER_DICT = {
    '1': {'name':'志军','age':18},
    '2': {'name':'大伟','age':48},
    '3': {'name':'梅凯','age':38},
}

@app.route('/login',methods=['GET',"POST"])
def login():
    if request.method == 'GET':
        return render_template('login.html')
    user = request.form.get('user') # 获取POST传过来的值
    pwd = request.form.get('pwd') # 获取POST传过来的值
    if user == 'alex' and pwd == '123':
        # 用户信息放入session
        session['user_info'] = user
        return redirect('/index')
    else:
        return render_template('login.html',msg ='用户名或密码错误')
        # return render_template('login.html',**{'msg':'用户名或密码错误'})


@app.route('/index',endpoint='n1')
def index():
    user_info = session.get('user_info')
    if not user_info:
        return redirect('/login')

    return render_template('index.html',user_dict = USER_DICT)

@app.route('/detail')
def detail():
    user_info = session.get('user_info')
    if not user_info:
        return redirect('/login')

    uid = request.args.get('uid')
    info = USER_DICT.get(uid)

    return render_template('detail.html',info = info)


@app.route('/logout')
def logout():
    del session['user_info']
    return redirect('/login')


if __name__ == '__main__':
    app.run()

templates\login.html

<!DOCTYPE html>
<html lang="zh-cn">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>休息系统-用户登录</title>
</head>
<body>
    <h1>登录</h1>
    <form method="post">
        <input type="text" name="user">
        <input type="password" name="pwd">
        <input type="submit" value="提交"> {{msg}}
    </form>
</body>
</html>

templates\index.html

<!DOCTYPE html>
<html lang="zh-cn">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Title</title>
</head>
<body>
    <ul>
        {% for k,v in user_dict.items() %}
            <li>{{v.name}} <a href="/detail?uid={{k}}">查看详细</a></li>
        {% endfor %}
    </ul>
</body>
</html>

templates\detail.html

<!DOCTYPE html>
<html lang="zh-cn">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Title</title>
</head>
<body>
    <h1>详细信息</h1>
    <div>{{info.name}}</div>
    <div>{{info.age}}</div>
</body>
</html>

app.run=>run_simple

from werkzeug.wrappers import Request, Response
from werkzeug.serving import run_simple


@Request.application
def hello(request):
    return Response('Hello World!')

if __name__ == '__main__':
    # 请求一旦到来,执行第三个参数    参数()
    run_simple('localhost', 4000, hello) # hello(xxx)
posted @ 2021-06-07 10:17  wuyuan2011woaini  阅读(27)  评论(0编辑  收藏  举报