眾所周知,flask使用的模板語言是jinja2,關於jinja的快速入門可以看 https://spacewander.github.io/explore-flask-zh/8-templates.html。
我在模板中使用{{}}來獲取變量沒有問題,在模板頁面里使用JavaScript函數也沒有問題。有幾個需要注意的點:
- js 函數中,ajax請求重定向。
function sale_submit(obj){
var words='a';
var page=2;
window.location.href ="{{ url_for('main.search_material_list',words=words,page=page)}}"
}
Jinja不能使用Javascript變量。這樣是接收不到指定的參數。
只要改成
window.location.href ="{{ url_for('main.search_material_list')}}"+'?page='+page+'&words='+words
就可以成功發送和接收參數。
不過如果js不是寫在html頁面里,而是作為單獨的.js外連接,那么是無法使用jinja的template語法的,像是{{}}這些都無法使用。