我是靠谱客的博主 自然身影,这篇文章主要介绍AssertionError: View function mapping is overwriting an existing endpoint function: login解决方法,现在分享给大家,希望可以做个参考。

这几天在学习flask的时候,被AssertionError: View function mapping is overwriting an existing endpoint function: login卡了很久。

这个错误的引起的原因是定义了多个同名的视图函数,但我的程序一直只有唯一一个视图函数

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
from flask import render_template, flash, redirect from functools import wraps from app import app from app.forms import LoginForm #def test(func): #@wraps(func) @app.route('/login', methods=['GET', 'POST']) def login(): form = LoginForm() return render_template('login.html', title = 'Sign In', form = form)

只有login()函数,期间试过不断更换函数名各种方法均未解决,直到在Google上看到一篇大佬的文章给了灵感。

引发错误的原因是装饰器的链式调用,具体解决方法使用模块funtools.wraps方法,这是Python自带的一种方法。

这是在stackoverflow上一个使用例子。

我自己的解决代码:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
from flask import render_template, flash, redirect from functools import wraps from app import app from app.forms import LoginForm def test(func): @wraps(func) @app.route('/login', methods=['GET', 'POST']) def login(): form = LoginForm() return render_template('login.html', title = 'Sign In', form = form)

在login视图函数的基础上外加一个test函数,传入func参数,因为wraps必须要一个参数才能工作。问题解决。

这是成功运行生成的表单。

http://trytofix.github.io/2016/05/05/Flask%E4%B8%AD%E4%BD%BF%E7%94%A8%E8%A3%85%E9%A5%B0%E5%99%A8%E9%81%87%E5%88%B0%E7%9A%84%E9%97%AE%E9%A2%98AssertionError-View-function-mapping-is-overwriting-an-existing-endpoint-function/

https://stackoverflow.com/questions/31900579/view-function-mapping-is-overwriting-an-existing-endpoint-function-when-using-a

以上是参考资料。

最后

以上就是自然身影最近收集整理的关于AssertionError: View function mapping is overwriting an existing endpoint function: login解决方法的全部内容,更多相关AssertionError:内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(65)

评论列表共有 0 条评论

立即
投稿
返回
顶部