当前位置: 移动技术网 > IT编程>脚本编程>Python > 【Flask】报错解决方法:AssertionError: View function mapping is overwriting an existing endpoint function: main.user

【Flask】报错解决方法:AssertionError: View function mapping is overwriting an existing endpoint function: main.user

2019年04月20日  | 移动技术网IT编程  | 我要评论

地铁笨蛋4小游戏,东南早报,盘龙血族

  

  运行flask时出现了一个错误, assertionerror: view function mapping is overwriting an existing endpoint function: main.user

  直译就是视图方法中重写了一个存在的endpoint方法。那么问题来了,endpoint 是何方神圣?

  查看了下源码,它的本质其实是请求url的一个规则,用来标记请求之后由哪个方法去具体执行。

 

@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官方文档中的解释:

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()

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下代码,发现确实是定义了相同方法名。

#...

@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了,重命名其中一个方法名即可,问题搞定✿✿ヽ(°▽°)ノ✿

 

参考文档:

 

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网