我正在關注有關如何使用 Flask 以及如何在 HTML 文件中顯示字典 pinDict 中的值的教程。render_template當我導航到http://127.0.0.1:5000/21/on時,只出現“Hi”。為什么是這樣?#app.pyfrom flask import Flask, render_templateapp = Flask(__name__)pinDict = {"21": {"name": "GPIO 21", "state": "off"}}@app.route('/')def index(): return "hey"@app.route("/<changePin>/<action>")def turnOn(changePin, action): return render_template("home.html", **pinDict)if __name__ == '__main__': app.run(debug=True, host="0.0.0.0")我的 home.html 文件是這樣的:<!DOCTYPE html><html> <body id = "myButton"> <h1>RPi Web Server</h1> {% for pin in pinDict %} <h2> {{pinDict[pin]["name"]}} </h2> {% endfor %} <script> function printLog() { var myHTML = "<h1> Hi </h1>" document.getElementById("myButton").innerHTML = myHTML; } </script> <script>printLog()</script> </body></html>很簡單。有點困惑為什么它沒有顯示,因為這與我遵循的教程中的代碼幾乎完全相同。希望得到一些幫助,感謝您的寶貴時間。
1 回答

神不在的星期二
TA貢獻1963條經驗 獲得超6個贊
你最好使用
return render_template("home.html", pinDict=pinDict)
代替
return render_template("home.html", **pinDict)
添加回答
舉報
0/150
提交
取消