html
<form action="/login" method="POST"> <input type="text" name="username"> <input type="password" name="password"> <input type="submit" value="提交"> </form>
python
# -*- coding:utf-8 -*- from flask import Flask,session,redirect,url_for,request,render_template app = Flask(__name__) app.secret_key='123' #配置secret_key,否则不能实现session对话 @app.route('/') def index(): if session.get('username') == 'wanghao' and session.get('password') == '123': return "你已经登陆" msg="没有登陆" return render_template('from_login.html') @app.route("/login",methods=["POST","GET"]) def login(): if request.method=='POST': session['username']=request.form['username'] session['password']=request.form['password'] return redirect(url_for('index')) return '123' if __name__ == '__main__': app.debug=True app.run(port=7998)
>>> import os
>>> os.urandom(24)
'xcax0cx86x04x98@x02bx1b7x8cx88]x1bxd7"+xe6px@xc3#\' #生成随机的 secret_key