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

app = Flask(__name__)

app.secret_key = "sdfasdfasdf3fsdf"

@app.route('/')
def hello_world():
return 'Hello World!' def wapper(func):
def inner(*args,**kwargs):
if not session.get('user_info'):
return redirect('/login')
return func(*args,**kwargs)
return inner @app.route('/login',methods=['GET','POST'])
def login():
if request.method == "GET":
return render_template('login.html')
else:
user = request.form.get('username')
pwd = request.form.get('password')
if user == 'alex' and pwd == '':
session['user_info'] = user
return redirect('/index')
else:
return render_template('login.html',msg='用户或密码错误') @app.route('/index',methods=['GET'])
@wapper
def index():
return render_template('index.html') @app.route('/query',methods=['GET'])
def query():
if not session.get('user_info'):
return redirect('/login')
return 'query' @app.route('/student',methods=['GET'])
@wapper
def student():
return 'student' if __name__ == '__main__':
app.run()

上面方面使用装饰器会有一个弊端:

"AssertionError: View function mapping is overwriting an existing endpoint function"如何解决

为什么会出现这样的问题:

使用Flask定义URL的时候,如果出现"AssertionError: View function mapping is overwriting an existing endpoint function"这个异常信息,就说明定义了多个同名的视图函数,只需要改成不同的函数名即可。
这是为什么呢? 原来flask中url跟视图函数并不是直接对应的,而是有一个中间者-endpoint。 三者之间的关系是这样的: ```
url---->endpoint---->view_function
``` 它们是一对一的关系,在注册add_url_rule的时候,如果不指定endpoint,那么endpoint就会默认为函数名字,如果同一个endpoint于多个url注册的话,就会发生冲突,从而抛出异常。

解决方法:

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

app = Flask(__name__)

app.secret_key = "sdfasdfasdf3fsdf"

@app.route('/')
def hello_world():
return 'Hello World!' @app.route('/login',methods=['GET','POST'])
def login():
if request.method == "GET":
return render_template('login.html')
else:
user = request.form.get('username')
pwd = request.form.get('password')
if user == 'alex' and pwd == '':
session['user_info'] = user
return redirect('/index')
else:
return render_template('login.html',msg='用户或密码错误') def wapper(func):
def inner(*args,**kwargs):
if not session.get('user_info'):
return redirect('/login')
return func(*args,**kwargs)
return inner @app.route('/index',methods=['GET'],endpoint='index')
@wapper
def index():
return render_template('index.html') @app.route('/query',methods=['GET'],endpoint='query')
@wapper
def query():
if not session.get('user_info'):
return redirect('/login')
return 'query' @app.route('/student',methods=['GET'],endpoint='student')
@wapper
def student():
return 'student' if __name__ == '__main__':
app.run()

解决方法

  

Python Flask装饰器登录验证的更多相关文章

  1. 如何用python的装饰器定义一个像C++一样的强类型函数

        Python作为一个动态的脚本语言,其函数在定义时是不需要指出参数的类型,也不需要指出函数是否有返回值.本文将介绍如何使用python的装饰器来定义一个像C++那样的强类型函数.接下去,先介绍 ...

  2. 关于Python的装饰器(1)

    Python的装饰器的概念,一直有点微妙.之前在StackOverflow上看过一篇感觉说明的很清楚的介绍: *A decorator must accept a function as an arg ...

  3. 浅谈Django的中间件与Python的装饰器

    浅谈Django的中间件 与Python的装饰器 一.原理 1.装饰器是Python的一种语法应用,利用闭包的原理去更改一个函数的功能,即让一个函数执行之前先到另外一个函数中执行其他需求语句,在执行该 ...

  4. 使用 python 编写一个授权登录验证的模块

    使用 python 编写一个授权登录验证的模块 我们编写的思路: 1.登录的逻辑:如果用户名和密码正确,就返回 token . 2.生成 token 的逻辑,根据用户名,随机数,当前时间 + 2 小时 ...

  5. 练习:python 操作Mysql 实现登录验证 用户权限管理

    python 操作Mysql 实现登录验证 用户权限管理

  6. Python各式装饰器

    Python装饰器,分两部分,一是装饰器本身的定义,一是被装饰器对象的定义. 一.函数式装饰器:装饰器本身是一个函数. 1.装饰函数:被装饰对象是一个函数 [1]装饰器无参数: a.被装饰对象无参数: ...

  7. Python札记 -- 装饰器补充

    本随笔是对Python札记 -- 装饰器的一些补充. 使用装饰器的时候,被装饰函数的一些属性会丢失,比如如下代码: #!/usr/bin/env python def deco(func): def ...

  8. python基础——装饰器

    python基础——装饰器 由于函数也是一个对象,而且函数对象可以被赋值给变量,所以,通过变量也能调用该函数. >>> def now(): ... print('2015-3-25 ...

  9. 【转】详解Python的装饰器

    原文链接:http://python.jobbole.com/86717/ Python中的装饰器是你进入Python大门的一道坎,不管你跨不跨过去它都在那里. 为什么需要装饰器 我们假设你的程序实现 ...

随机推荐

  1. Hcharts和Echarts----制作报表的工具

    Hcharts官网:https://www.hcharts.cn/Hcharts API文档:https://api.hcharts.cn/highcharts Echarts官网:http://ec ...

  2. Linux 第30天: (08月5日) 练习和作业

    变量脚本 1.编写脚本/root/bin/systeminfo.sh,显示当前主机系统信息,包括主机名,IPv4地址,操作系统版本,内核版本,CPU型号,内存大小,硬盘大小 server_ip=`if ...

  3. expect使用小结

    因为工作关系,需要经常从线上机器上拉取数据,于是想着能否写个脚本,自动完成这个任务呢? 我一般使用scp在机器间传输文件,然而每次scp都需要输入密码,自动化脚本怎么解决这个问题呢?于是expect这 ...

  4. Nginx安装(官网翻译)

    转载自:https://www.nginx.com/resources/wiki/start/topics/tutorials/install/ 二进制版本预包装的Linux和BSD大多数Linux发 ...

  5. linux查看文件相关指令

    以下内容整理自以下两篇文章: http://www.cnblogs.com/xilifeng/archive/2012/10/13/2722596.html Linux 查看文件内容的命令 http: ...

  6. [洛谷P2704] [NOI2001]炮兵阵地

    洛谷题目链接:[NOI2001]炮兵阵地 题目描述 司令部的将军们打算在NM的网格地图上部署他们的炮兵部队.一个NM的地图由N行M列组成,地图的每一格可能是山地(用"H" 表示), ...

  7. CAS(硬件CPU同步原语)

    CAS有3个操作数.内存值V,旧的预约值A,要修改后的新值B.当且仅当预期值A和预期值V相同时,将内存值V修改为新值B.当且仅当预期值A和内存值V相同时,将内存值V修改为B,否则什么都不做. 应用1. ...

  8. Apache 文件服务器

    1.安装apache服务器yum install httpd 2.启动httpd服务service httpd start 3.查看httpd服务器的版本httpd -v 4.修改访问端口和文件路径, ...

  9. IConfigurationSectionHandler 接口

    IConfigurationSectionHandler 处理对特定的配置节的访问. 示例代码: public class MyConfig : IConfigurationSectionHandle ...

  10. 【usaco-Earthquake, 2001 Open】 0-1分数规划 & 最优比率生成树

    题意:给定n个点m条边,一开始这些边全都是断的,要修一些边使得n个点全部联通.修完一共可以得到F元,修一条边有成本di和时间ti,要使得 得到的钱数 / 总时间 这个比值最大. 参考资料: 红线内的内 ...