[python] AssertionError : 뷰 함수 매핑이 기존 끝점 함수를 덮어 씁니다 : main

이 같은 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 (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



답변

보기 이름은 동일한보기 방법을 가리키는 경우에도 고유해야합니다.

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'])


답변

이 같은 문제는 모듈에 API 함수가 두 개 이상 있고 각 함수를 2 개의 데코레이터로 래핑하려고 할 때 나에게 발생했습니다.

  1. @ app.route ()
  2. 내 맞춤 @exception_handler 데코레이터

이 두 데코레이터로 둘 이상의 함수를 래핑하려고 시도했기 때문에 이와 동일한 예외가 발생했습니다.

@app.route("/path1")
@exception_handler
def func1():
    pass

@app.route("/path2")
@exception_handler
def func2():
    pass

특히 이름 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

함수의 이름을 변경하면 문제가 해결되었습니다 ( 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를 사용하는 사용자 endpoint의 경우 Roei Bahumi가 언급 한 __name__것과 같은 값을 변경하는 것보다 키 인수를 사용하는 것이 좋습니다 . 그의 예를 들면 다음과 같습니다.

@app.route("/path1", endpoint='func1')
@exception_handler
def func1():
    pass

@app.route("/path2", endpoint='func2')
@exception_handler
def func2():
    pass


답변

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"])


답변

나는 이것에 더 많은 ‘템플릿’유형의 솔루션을 추가하고 싶습니다.

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

최근에 찾은 “Demystifying Decorators”라는 정말 흥미로운 기사를 추가하고 싶습니다 : https://sumit-ghosh.com/articles/demystifying-decorators-python/


답변

이것은 다른 경로에 동일한 함수 이름이있는 경우에도 발생할 수 있습니다.


답변

고유 한 엔드 포인트 이름이 있다고 생각하지만 여전히이 오류가 발생하면 문제 가있는 것입니다 . 저도 마찬가지였습니다.

이 문제는 플라스크 0.10에서 발생하며 동일한 버전을 사용하는 경우 다음을 수행하여 제거하십시오.

sudo pip uninstall flask
sudo pip install flask=0.9