Python 断言错误:视图函数映射正在覆盖现有的端点函数:main
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17256602/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
AssertionError: View function mapping is overwriting an existing endpoint function: main
提问by Kimmy
Does anyone know why I can't overwrite an existing endpoint function if i have two url rules like this
有谁知道如果我有两个这样的 url 规则,为什么我不能覆盖现有的端点函数
app.add_url_rule('/',
view_func=Main.as_view('main'),
methods=["GET"])
app.add_url_rule('/<page>/',
view_func=Main.as_view('main'),
methods=["GET"])
Traceback:
追溯:
Traceback (most recent call last):
File "demo.py", line 20, in <module> methods=["GET"])
File ".../python2.6/site-packages/flask??/app.py",
line 62, in wrapper_func return f(self, *args, **kwargs)
File ".../python2.6/site-packages/flask??/app.py",
line 984, in add_url_rule 'existing endpoint function: %s' % endpoint)
AssertionError: View function mapping is overwriting an existing endpoint
function: main
采纳答案by Michael Davis
Your view names need to be unique even if they are pointing to the same view method.
即使它们指向相同的视图方法,您的视图名称也必须是唯一的。
app.add_url_rule('/',
view_func=Main.as_view('main'),
methods = ['GET'])
app.add_url_rule('/<page>/',
view_func=Main.as_view('page'),
methods = ['GET'])
回答by yassen
There is a fix for Flask issue #570 introduced recenty (flask 0.10) that causes this exception to be raised.
最近(flask 0.10)引入了 Flask 问题 #570 的修复程序,该修复程序导致引发此异常。
See https://github.com/mitsuhiko/flask/issues/796
见https://github.com/mitsuhiko/flask/issues/796
So if you go to flask/app.py and comment out the 4 lines 948..951, this may help until the issue is resovled fully in a new version.
因此,如果您转到 flask/app.py 并注释掉 4 行 948..951,这可能会有所帮助,直到问题在新版本中完全解决。
The diff of that change is here: http://github.com/mitsuhiko/flask/commit/661ee54bc2bc1ea0763ac9c226f8e14bb0beb5b1
该更改的差异在这里:http: //github.com/mitsuhiko/flask/commit/661ee54bc2bc1ea0763ac9c226f8e14bb0beb5b1
回答by dtheodor
Flask requires you to associate a single 'view function' with an 'endpoint'. You are calling Main.as_view('main')twice which creates two different functions (exactly the same functionality but different in memory signature). Short story, you should simply do
Flask 要求您将单个“视图函数”与“端点”相关联。您正在调用Main.as_view('main')两次,这会创建两个不同的函数(完全相同的功能,但内存签名不同)。短篇小说,你应该简单地做
main_view_func = Main.as_view('main')
app.add_url_rule('/',
view_func=main_view_func,
methods=["GET"])
app.add_url_rule('/<page>/',
view_func=main_view_func,
methods=["GET"])
回答by Roei Bahumi
This same issue happened to me when I had more than one API function in the module and tried to wrap each function with 2 decorators:
当我在模块中有多个 API 函数并尝试用 2 个装饰器包装每个函数时,同样的问题发生在我身上:
- @app.route()
- My custom @exception_handler decorator
- @app.route()
- 我的自定义 @exception_handler 装饰器
I got this same exception because I tried to wrap more than one function with those two decorators:
我遇到了同样的异常,因为我试图用这两个装饰器包装多个函数:
@app.route("/path1")
@exception_handler
def func1():
pass
@app.route("/path2")
@exception_handler
def func2():
pass
Specifically, it is caused by trying to register a few functions with the name wrapper:
具体来说,它是由于尝试使用名称wrapper注册一些函数引起的:
def exception_handler(func):
def wrapper(*args, **kwargs):
try:
return func(*args, **kwargs)
except Exception as e:
error_code = getattr(e, "code", 500)
logger.exception("Service exception: %s", e)
r = dict_to_json({"message": e.message, "matches": e.message, "error_code": error_code})
return Response(r, status=error_code, mimetype='application/json')
return wrapper
Changing the name of the function solved it for me (wrapper.__name__ = func.__name__):
更改函数的名称为我解决了这个问题(wrapper.__name__ = func.__name__):
def exception_handler(func):
def wrapper(*args, **kwargs):
try:
return func(*args, **kwargs)
except Exception as e:
error_code = getattr(e, "code", 500)
logger.exception("Service exception: %s", e)
r = dict_to_json({"message": e.message, "matches": e.message, "error_code": error_code})
return Response(r, status=error_code, mimetype='application/json')
# Renaming the function name:
wrapper.__name__ = func.__name__
return wrapper
Then, decorating more than one endpoint worked.
然后,装饰不止一个端点起作用了。
回答by Uri Shalit
For users that use @app.route it is better to use the key-argument endpointrather then chaning the value of __name__like Roei Bahumistated. Taking his example will be:
对于使用@app.route 的用户,最好使用键参数endpoint而不是__name__像Roei Bahumi所说的那样更改值。以他的为例将是:
@app.route("/path1", endpoint='func1')
@exception_handler
def func1():
pass
@app.route("/path2", endpoint='func2')
@exception_handler
def func2():
pass
回答by shrishinde
If you think you have unique endpoint names and still this error is given then probably you are facing issue. Same was the case with me.
如果您认为您有唯一的端点名称,但仍然出现此错误,那么您可能遇到了问题。我的情况也是如此。
This issue is with flask 0.10 in case you have same version then do following to get rid of this:
如果您有相同的版本,则此问题与 Flask 0.10 有关,然后执行以下操作以摆脱此问题:
sudo pip uninstall flask
sudo pip install flask=0.9
回答by imbatman
I would just like to add to this a more 'template' type solution.
我只想添加一个更“模板”类型的解决方案。
def func_name(f):
def wrap(*args, **kwargs):
if condition:
pass
else:
whatever you want
return f(*args, **kwargs)
wrap.__name__ = f.__name__
return wrap
would just like to add a really interesting article "Demystifying Decorators" I found recently: https://sumit-ghosh.com/articles/demystifying-decorators-python/
只想添加一篇我最近发现的非常有趣的文章“Demystifying Decorators”:https: //sumit-ghosh.com/articles/demystifying-decorators-python/
回答by Andriy Shchudlo
Your view names need to be unique even if they are pointing to the same view method, or you can add from functools import wraps and use @wraps https://docs.python.org/2/library/functools.html
即使它们指向相同的视图方法,您的视图名称也必须是唯一的,或者您可以添加 from functools import wraps 并使用 @wraps https://docs.python.org/2/library/functools.html
回答by Joshua Johns
use flask 0.9 instead
use the following commands
sudo pip uninstall flask
使用 Flask 0.9 代替使用以下命令
sudo pip uninstall flask
sudo pip install flask==0.9
回答by Joseph
This can happen also when you have identical function names on different routes.
当您在不同的路由上具有相同的函数名称时,也会发生这种情况。

