Flask+简单的登录案例
app.py
from flask import Flask, render_template, redirect, session from flask import request app = Flask(__name__) app.secret_key = '\xc9ixnRb\xe40\xd4\xa5\x7f\x03\xd0y6\x01\x1f\x96\xeao+\x8a\x9f\xe4' @app.route('/', methods=['GET', 'POST']) def login(): if request.method == 'GET': return render_template('login.html') user = request.form.get('user') pwd = request.form.get('pwd') print(pwd) if user == 'kevin' and pwd == '123': session['user_info'] = user return redirect('/index') else: return render_template('login.html', msg='用户名或密码错误') @app.route('/index', methods=['GET']) def index(): user_info = session.get('user_info') if not user_info: return redirect('/login') return "欢迎登录" if __name__ == '__main__': app.run()
login.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>LOGIN</title> </head> <body> <form method="post"> <input type="text" name="user"> <input type="password" name="pwd"> <input type="submit" name="submit">{{ msg }} </form> </body> </html>
运行结果:
本文来自博客园,作者:江雪独钓翁,转载请注明原文链接:https://www.cnblogs.com/zhouwp/p/15629482.html