【问题标题】:werkzeug.routing.BuildError coming for Flask appFlask 应用程序出现 werkzeug.routing.BuildError
【发布时间】:2020-07-11 08:06:30
【问题描述】:

我在尝试运行我的 Flask 应用程序时遇到错误:BuildError: 无法使用值 ['resultFound'] 为端点 'result' 构建 url。您指的是“菜单”吗?

问题与 POST 的调用有关。我附上了与此错误相关的 4 个文件的代码,但我省略了导入包和文件的其他部分。非常感谢您的帮助。非常感谢。如果您想要任何其他代码,我可以添加它。

这是我运行烧瓶应用程序的主要 python 函数。

@app.route("/search", methods=["POST", "GET"])
def search():
    if request.method == "POST":
        user = request.form["searching"]
        return redirect(url_for('result', resultFound = user))
    else:
        return render_template("search.html")

app.route("/<resultFound>")
def result(resultFound):
    return render_template('result.html', nameartist = artistName(resultFound), numfollowers = artistfollower(resultFound))

这是一个 python 文件,它使用来自 search.html 的输入值获取 results.html 的信息。

def artists(searchinput):
    searchResults = spotifyObject.search(searchinput,1,0,"artist")
    artist = searchResults['artists']['items'][0]
    return artist

def artistname(inputvalue):
    value = artists(inputvalue)
    artistName = value['name']
    return artistName

def artistfollower(inputvalue):
    value = artists(inputvalue)
    artistfollowers = value['followers']['total']
    return artistfollowers

这是获取输入值的 search.html。

   <form action="#" method="post">
       <input type="text" id="myText" name="searching" value="input artist">
       <p><input type="submit" value="submit" /></p>
   </form>

这是使用来自 search.html 的输入值并在 python 文件的帮助下获取数据的 result.html。

  <p>The artist {{ nameartist }} has {{ numfollowers }} followers.</p>

【问题讨论】:

  • 尝试打印调用request.form['searching'] 后得到的user,看看是否得到了值。因为如果你将None 作为result 的函数参数传递,flask 将不知道在没有参数的情况下将result 绑定到哪里。

标签: python html flask post


【解决方案1】:
BuildError: Could not build url for endpoint 'result' with values ['resultFound'].

因为您在result 路由装饰器中缺少@@app 而不是app

@app.route("/<resultFound>")
def result(resultFound):
    [..]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-04-02
    • 1970-01-01
    • 2017-02-18
    • 2020-01-03
    • 2018-10-02
    • 2017-05-17
    • 2016-05-05
    • 1970-01-01
    相关资源
    最近更新 更多