flask模板template 與js 的一些問題


眾所周知,flask使用的模板語言是jinja2,關於jinja的快速入門可以看 https://spacewander.github.io/explore-flask-zh/8-templates.html。
我在模板中使用{{}}來獲取變量沒有問題,在模板頁面里使用JavaScript函數也沒有問題。有幾個需要注意的點:

  1. 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語法的,像是{{}}這些都無法使用。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM