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. marquee标签实现跑马灯效果--无缝滚动

    今天在做微信端的大转盘抽奖时,想把所有用户的抽奖记录做成无缝滚动的效果,无奈我的js功底太差,一时想不出实现的方法,便百度各种相似效果.但无意中发现了一个html标签——<marquee> ...

  2. Java入门第三季——Java中的集合框架(中):Map&HashMap

    package com.imooc.collection; import java.util.HashSet; import java.util.Set; /** * 学生类 * @author Ad ...

  3. solr不是自启动,添加code失败

    原文:https://blog.csdn.net/qq_30242987/article/details/100044964 我主要的问题是  conf要复制  configests/sample_t ...

  4. Ubuntu14版桌面突然卡住怎么办

    参考:https://blog.csdn.net/hautxuhaihu/article/details/78924926 (1)ctrl+alt+f1...6进入命令行终端.用户名,密码登录. (2 ...

  5. [2019HDU多校第一场][HDU 6590][M. Code]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6590 题目大意(来自队友):二维平面上有\(n\)个点,每个点要么是黑色要么是白色,问能否找到一条直线 ...

  6. [2019牛客多校第二场][G. Polygons]

    题目链接:https://ac.nowcoder.com/acm/contest/882/G 题目大意:有\(n\)条直线将平面分成若干个区域,要求处理\(m\)次询问:求第\(q\)大的区域面积.保 ...

  7. SQL Server查询表结构语句

    --1:获取当前数据库中的所有用户表   www.2cto.com   select Name from sysobjects where xtype='u' and status>=0  -- ...

  8. 用CSS实现梯形图标

    遇到需要实现如下图标 由图形分析,梯形,平行四边形等都可以由矩形变形而来. 而想要实现梯形,需要进行3D变换,需要使用css3的 perspective属性. 属性 perspective指定了观察者 ...

  9. org.apache.commons.io.FilenameUtils 常用的方法

    /** * getExtension * 获取文件的后缀名 */ public static void testGetExtension() { String extension = Filename ...

  10. Little Prince

    You know — one loves the sunset, when one is so sad... 你知道的—当一个人情绪低落的时候,他会格外喜欢看日落...... If someone l ...