運行Flask時出現了一個錯誤, AssertionError: View function mapping is overwriting an existing endpoint function: main.userhtml
直譯就是視圖方法中重寫了一個存在的endpoint方法。那麼問題來了,endpoint 是何方神聖?python
查看了下源碼,它的本質實際上是請求url的一個規則,用來標記請求以後由哪一個方法去具體執行。flask
@property def endpoint(self): """The endpoint that matched the request. This in combination with :attr:`view_args` can be used to reconstruct the same or a modified URL. If an exception happened when matching, this will be ``None``. """ if self.url_rule is not None: return self.url_rule.endpoint
Flask官方文檔中的解釋:api
endpoint(endpoint) A decorator to register a function as an endpoint. Example: @app.endpoint('example.endpoint') def example(): return "example" Parameters: endpoint – the name of the endpoint
以及其餘函數中的用法,例如:add_url_rule()app
add_url_rule(rule, endpoint=None,...) Parameters: #... endpoint – the endpoint for the registered URL rule. Flask itself assumes the name of the view function as endpoint
敲黑板劃重點,Flask的默認endpoint其實就是視圖模塊中的各個具體方法名。函數
弄明白了endpoint,從新review下代碼,發現確實是定義了相同方法名。this
#... @main.route('/user/<name>') def user(name): return render_template('user_simple.html',name=name) #... @main.route('/user/<username>') def user(username): user = User.query.filter_by(username=username).first_or_404() return render_template('user.html',user=user)
找到問題根因,解決方法就so easy了,重命名其中一個方法名便可,問題搞定✿✿ヽ(°▽°)ノ✿url