1.装饰器装饰多个视图函数出现的问题

代码实例:

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

app = Flask(__name__)
app.secret_key = "$%@&!**" # $%@&!**是加密字符串,可随意设置 @app.route("/login", methods=["GET", "POST"])
def login():
if request.method == "GET":
return render_template("login.html")
else:
session["username"] = request.form.get("username")
return redirect("/detail") # 自定义装饰器
def outer(func):
def inner(*args, **kwargs):
if session.get("username"):
ret = func(*args, **kwargs)
return ret
else:
return redirect("/login")
return inner @app.route("/detail", methods=["GET", ])
@outer
def detail():
return render_template("detail.html") @app.route("/detail2", methods=["GET", ])
@outer
def detail2():
return "this is detail2" if __name__ == '__main__':
app.run(debug=True) """
执行结果:
AssertionError: View function mapping is overwriting an existing endpoint function: inner 结论: 用装饰器装饰多个视图函数会抛出异常, 原因是多次出现了innner函数,产生覆盖现象
"""

2.使用装饰器修复技术解决该问题

from flask import Flask, request, render_template, session, redirect
from functools import wraps app = Flask(__name__) app.secret_key = "~!@#$%^" @app.route("/login", methods=["GET", "POST"])
def login():
if request.method == "GET":
return render_template("login.html")
else:
session["username"] = request.form.get("username")
return redirect("/detail") # 自定义装饰器
def outer(func):
@wraps(func) # 装饰器修复技术
def inner(*args, **kwargs):
if session.get("username"):
ret = func(*args, **kwargs)
return ret
else:
return redirect("/login") return inner @app.route("/detail", methods=["GET", ])
@outer
def detail():
return "this is detail" @app.route("/detail2", methods=["GET", ])
@outer
def detail2():
return "this is detail2" if __name__ == '__main__':
app.run()

flask(1.1)装饰器装饰多个视图函数出现的问题的更多相关文章

  1. python27期day14:有参装饰器、多个装饰器装饰一个函数、递归、作业题

    1.有参装饰器:给装饰器添加一个参数.来控制装饰器的行为. @auth(参数) auth里层的函数名 = auth(参数) 被装饰的函数名 = auth里层的函数名(被装饰的函数名) 被装饰的函数名( ...

  2. Python 装饰器装饰类中的方法

    title: Python 装饰器装饰类中的方法 comments: true date: 2017-04-17 20:44:31 tags: ['Python', 'Decorate'] categ ...

  3. Python_函数的有用信息、带参数的装饰器、多个装饰器装饰一个函数

    函数的有用信息 代码1: def login(username, password): """ 此函数需要用户名,密码两个参数,完成的是登录的功能. :return: T ...

  4. python 装饰器--对有无参数的函数进行装饰

    # 使用装饰器无参数的函数进行装饰# def func(funcionName): # print('-----1------') # def func_in(): # print('--func_i ...

  5. python 全栈开发,Day12(函数的有用信息,带参数的装饰器,多个装饰器装饰一个函数)

    函数的执行时,*打散.函数的定义时,*聚合. from functools import wraps def wrapper(f): # f = func1 @wraps(f) def inner(* ...

  6. python全栈开发day12-函数的有用信息、带参数的装饰器、多个装饰器装饰一个函数、global和nonlocal的进一步解析和总结

    1.上周回顾 1).函数名的应用 直接打印函数名,是函数的地址 变量 函数的参数 函数的返回值 可以当容器类数据类型的元素 2).闭包 内层函数对外层函数的非全局变量的引用,就是闭包. 并返回内部函数 ...

  7. python-day14--带参数的装饰器+多个装饰器装饰同一个函数

    1.# 带参数的装饰器def f1(flag): def f2(func): def inner(*args,**kwargs): if flag: '''执行函数之前要做的''' r=func(*a ...

  8. python 带参与不带参装饰器的使用与流程分析/什么是装饰器/装饰器使用注意事项

    一.什么是装饰器 装饰器是用来给函数动态的添加功能的一种技术,属于一种语法糖.通俗一点讲就是:在不会影响原有函数的功能基础上,在原有函数的执行过程中额外的添加上另外一段处理逻辑 二.装饰器功能实现的技 ...

  9. functools.wraps 带参数的装饰器 多个装饰器装饰同一个函数

    装饰器开发原则 : 开放封闭原则装饰器的作用 :在不改变原函数的调用方式的情况下,在函数的前后添加功能装饰器的本质 : 闭包函数 def wrapper(func): def inner(*args, ...

随机推荐

  1. python——flask常见接口开发(简单案例)

    python——flask常见接口开发(简单案例)原创 大蛇王 发布于2019-01-24 11:34:06 阅读数 5208 收藏展开 版本:python3.5+ 模块:flask 目标:开发一个只 ...

  2. drf 第一节

    drf django-restframework ''' 1.接口:接口的概念.数据接口文档.接口规范(restful).Postman接口测试工具 2.drf请求生命周期 - CBV 3.drf的基 ...

  3. C# 通过Process.Start() 打开程序 置顶方法

    private void webBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e) { try { foreach ...

  4. 查看nginx服务器状态

    编译安装时使用--with-http_stub_status_module开启状态页面模块 [root@proxy ~]# yum -y install gcc pcre-devel openssl- ...

  5. sp_dboption

    http://www.yesky.com/imagesnew/software/tsql/ts_sp_da-di_8c32.htm A. ½«Êý¾Ý¿âÉèÖÃΪֻ¶Á ÏÂÃæµÄʾÀý½« ...

  6. 并发编程入门(三): 使用C++11实现无锁stack(lock-free stack)

    前几篇文章,我们讨论了如何使用mutex保护数据及使用使用condition variable在多线程中进行同步.然而,使用mutex将会导致一下问题: 等待互斥锁会消耗宝贵的时间 - 有时候是很多时 ...

  7. Django从Models 10分钟定制一个Admin后台

    目录 Django从Models 10分钟建立一套RestfulApi Django从Models 10分钟定制一个Admin后台 简介 Django自带一个Admin后台, 支持用户创建,权限配置和 ...

  8. 关于Map的问题

    关于Map的问题主要有: (1)什么是散列表? (2)怎么实现一个散列表? (3)java中HashMap实现方式的演进? (4)HashMap的容量有什么特点? (5)HashMap是怎么进行扩容的 ...

  9. 最近公共祖先lca模板

    void dfs(int x,int root){//预处理fa和dep数组 fa[x][0]=root; dep[x]=dep[root]+1; for(int i=1;(1<<i)&l ...

  10. JVM GC之垃圾收集算法

    1.垃圾收集概念 GC目的 分配内存,为每个新建的对象分配空间 确保还在使用的对象的内存一直还在,不能把有用的空间当垃圾回收了 释放不再使用的对象所占用的空间 我们把还被引用的对象称为活的,把不再被引 ...