当前位置:网站首页>flask装饰器版登录、session
flask装饰器版登录、session
2022-08-10 18:39:00 【低调说】
登录
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>登录</title>
</head>
<body>
<h1>登录</h1>
<form method="post" action="/login">
<label>用户名
<input type="text" name="username">
</label>
<label>密码
<input type="=password" name="pwd">
</label>
<input type="submit" value="提交">{
{ msg }}
</form>
</body>
</html>
index
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<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>
detail
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>详细信息</h1>
<ul>
<li>{
{ info.name }}</li>
<li>{
{ info.age }}</li>
<li>{
{ info.nackname }}</li>
</ul>
</body>
</html>
flask代码
import functools
# 这个funtools这个跟装饰器登录有关,我没弄明白
# 1.装饰器写到路由下面
# 2.加了装饰器后,相当是改名了,得加一个endpoint
from flask import Flask, render_template, redirect, request, session
app = Flask(__name__)
app.secret_key = 'xdfegrhrghhbb'
app.debug = True
USER_DICT = {
'1': {
'name': 'dc', 'age': 18, 'username': 'baozi', 'mp': 'dffttyh', 'nackname': '包子'},
'2': {
'name': 'bb', 'age': 19, 'username': 'larou', 'mp': 'ggrgth', 'nackname': '腊肉'},
}
@app.route('/')
def hello_world():
return render_template('login.html')
def wapper(func):
# @wraps(func)
functools.wraps(func)
def inner(*args, **kwargs):
if not session.get('user_info'):
return redirect('/login')
return func(*args, **kwargs)
return inner
# @app.route('/index')
# def index():
# if not session.get('user_info'):
# return redirect('/login')
# return render_template('index.html', user_dict=USER_DICT)
@app.route('/index', endpoint='index')
@wapper
def index():
return render_template('index.html', user_dict=USER_DICT)
# @app.route('/detail/')
# def detail():
# if not session.get('user_info'):
# return redirect('/login')
# uid = request.args.get('uid')
# info = USER_DICT.get(uid)
# return render_template('detail.html', info=info)
@app.route('/detail/')
@wapper
def detail():
userid = request.args.get('uid')
info = USER_DICT.get(userid)
return render_template('detail.html', info=info)
@app.route('/login', methods=['GET', 'POST'])
def login():
if request.method == 'GET':
return render_template('login.html')
else:
username = request.form.get('username')
password = request.form.get('pwd')
if username == USER_DICT['1']['username'] and password == USER_DICT['1']['mp']:
session['user_info'] = username
return redirect('/index')
else:
return render_template('login.html', msg='用户名或密码不正确')
@app.route('/logout')
def logout():
del session['user_info']
return redirect('/login')
if __name__ == '__main__':
app.run()
边栏推荐
猜你喜欢
随机推荐
陕西CAS:1244028-50-9_Biotin-PEG3-SCO-PPh3 固体
关于npm/cnpm/npx/pnpm与yarn
[Image segmentation] Image segmentation based on cellular automata with matlab code
Unity_Stack<T>()的应用(多个次级界面后的返回逻辑)
120Hz OLED拒绝“烧屏”!华硕无双全能轻薄本
幕维三维动画——港珠澳大桥沉管安装三维动画实况
RS-485多主机通信的组网方式评估
6-11 先序输出叶结点(15分)
6-12 二叉搜索树的操作集(30分)
FPGA工程师面试试题集锦91~100
The servlet mapping path matching resolution
含有PEG 间隔基和一个末端伯胺基团(CAS:1006592-62-6)化学试剂
DefaultSelectStrategy NIOEventLoop执行策略
状态压缩dp蒙德里安的梦想
L2-035 完全二叉树的层序遍历
大家要的Biotin-PEG3-Br/acid/NHS ester/alcohol/amine合集分享
【图像去雾】基于颜色衰减先验的图像去雾附matlab代码
6-10 二分查找(20分)
请问下在datastream中用flinkcdc怎么设置jdbc的参数useSSL=false呀
第14章_MySQL事务日志









