왜 내가 아는 사람 can& # 39, 이렇게 할 경우 기존 종단점이나 t 덮어쓰시겠습니까 uirl 규칙요 두 함수
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 (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
이와 같은 문제로 상태입니까 때 나를 내가 있는 둘 이상의 API 함수를 사용하여 각 기능을 2 데코레이터 했고 모듈에서는 마감한다.
@app.route () 1. 2. 데코레이터 @exception_handler 내 사용자 정의
내가 맡을게 랩 같은 둘 이상의 기능이 있기 때문에 이 두 데코레이터 idfsysobject. 찾았다.
@app.route("/path1")
@exception_handler
def func1():
pass
@app.route("/path2")
@exception_handler
def func2():
pass
특히, 이는 몇 가지 기능을 하는 인한 등록 이름을 래퍼 :
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
변화하는 함수명 해결 it for me ( 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
그런 다음 두 개 이상의 종단점이나 꾸미는 했다.
@App.route 사용하는 사용자를 위한 것이 아니라 '' 다음 '가치' 와 같은 사용합니다를 주요 인수 종단점이나 차닝 name [뤠이 바이미] (https://stackoverflow.com/a/42254713/5019818) 고 밝혔다. 취임 예제에서와 것입니다.
@app.route("/path1", endpoint='func1')
@exception_handler
def func1():
pass
@app.route("/path2", endpoint='func2')
@exception_handler
def func2():
pass
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"])
난 이 & # 39 더 추가할 마찬가지로 template& # 39;; 유형 솔루션입니다.
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
마찬가지로, 확실히 알기 위해 아주 흥미로운 기사 " Decorators" 추가하기에서는 것이다. 최근 찾았다. https://sumit-ghosh.com/articles/demystifying-decorators-python/
Https://github.com/mitsuhiko/flask/issues/796 참조
그래서 가면 flask/app.py 및 4 선, 이 문제는 완전히 끝날 때까지 레소프레드 설명줄로 948.951 도울 수 있는 새로운 버전.
Diff 는 해당 변경할 수 있습니다. http://github.com/mitsuhiko/flask/commit/661ee54bc2bc1ea0763ac9c226f8e14bb0beb5b1
파악할 필요가 있어도 고유해야 가리키는 이름이 동일한 뷰를 통해 펑스올스 가져오기의 @wraps 둘러싸여집니다 및 사용 방법, 또는 추가할 수 있습니다. https://docs.python.org/2/library/functools.html