[英]How to pass a Python variable/Jinja2 template to a JavaScript file?
[英]Pass python list to JavaScript in a jinja template
我正在使用 flask 構建一個 CRUD 應用程序。 我完成了創建、讀取和刪除。 留下更新部分。 要更新的記錄將顯示在 html 表格中,以便編輯顯示的數據庫記錄。
我的問題是當數據庫記錄顯示在表單中時,我希望根據數據庫記錄中的值選中一個復選框或選擇一個下拉列表項。
Is there a way to pass the result of a database query (1 record) to JavaScript as a JavaScript array, obtain the items in the JavaScript array, and use them in JavaScript statements to control check boxes?
@app.route('/update/<int:id>/')
def update(id):
cur=mysql.connection.cursor()
cur.execute("SELECT * FROM profiles where entity_id = %s", (id,))
row=list(cur.fetchall())
cur.close()
return render_template('profile-update.html', data = row)
下面是模板
<!DOCTYPE html>
<html>
<head>
</head>
<body class="hold-transition skin-blue layout-top-nav">
<div class="form-check form-check-inline">
<input class="form-check-input" type="radio" name="inlineRadioOptions"
id="inlineRadio1" value="option1">
<label class="form-check-label" for="inlineRadio1">company</label>
</div>
<div class="form-check form-check-inline">
<input class="form-check-input" type="radio" name="inlineRadioOptions"
id="inlineRadio2" value="option2">
<label class="form-check-label" for="inlineRadio2">partnership</label>
</div>
<div class="form-check form-check-inline">
<input class="form-check-input" type="radio" name="inlineRadioOptions"
id="inlineRadio3" value="option3"`enter code here`>
</div>
</body>
</html>
我對此進行了廣泛的研究,但建議的解決方案似乎不起作用
您使用jinja 標記傳遞到模板中的數據。
/templates/demo_template.html
*{{data}}*
/main.py
import flask
app = flask.Flask(__name__)
@app.route('/demo')
def demo():
data = [dict(greeting='hello!'),dict(greeting='world!')]
return flask.render_template('demo_template.html', data=data)
if __name__ == "__main__":
app.run()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.