【发布时间】: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绑定到哪里。