前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【Flask】报错解决方法:Assert

【Flask】报错解决方法:Assert

作者头像
py3study
发布2020-01-16 17:04:00
6930
发布2020-01-16 17:04:00
举报
文章被收录于专栏:python3python3python3

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

参考文档:http://flask.pocoo.org/docs/1.0/api/

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-05-28 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档