Flask学习

Flask框架,简直是轻量级。。

Flask的Hello World

from flask import Flask

app = Flask(__name__)


@app.route('/index')
def index():
    return 'Hello Flask'


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

实现用户登录的实例

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

app = Flask(__name__)
app.secret_key = 'fdsa'
app.debug = True


@app.route('/login', methods=['GET', 'POST'])
def login():
    if request.method == 'GET':
        return render_template('login.html')
    usr = request.form.get('user')
    pwd = request.form.get('password')
    if usr == 'nihao' and pwd == '123':
        session['user_info'] = usr
        return redirect('/index')
    else:
        return render_template('login.html', msg='用户名密码错误')
        # 两种传值方式 取其一
        # return render_template('login.html',**{'msg':'用户名密码错误'})


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

    return '欢迎登录'


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


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

 

学习记录~

posted @ 2021-09-23 17:45  木_糖  阅读(36)  评论(0编辑  收藏  举报