【Python】flask url

【Python】flask url,第1张

url_for()方法在html文件中、html的标签中都可以直接使用,在单独js文件中好像不能直接使用。

详细描述:【Python】flask url_for()方法,在js中如何使用 - H5W3

flask相对路径的使用:

比如我想在ndvi.html中使用 static中images的png图片,那么则需要这样使用:







或者我想在showimg.js中更改ndvi.html中img的src属性,那么一定不能使用url_for,不起作用,

应该使用和上面👆一样的相对路径:

//无效写法,单独的js文件无法使用url_for()
var img = document.getElementById("img");
var path = "{{ url_for('static', filename='images/2010_01.png') }}";
img.src = path;
//这里如果输出img.src的值可以看到如下结果:
//  /%7B%7Burl_for('static',filename='images/2010_01.png')%7D%7D
//可以看到img.src的值为字符串,不是路径,并且一些符号也被更换了

//正确写法
var img = document.getElementById("img");
var path = "/static/images/2010_01.png","/static/images/2010_02.png";
img.src = path;

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1320812.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-11
下一篇 2022-06-11

发表评论

登录后才能评论

评论列表(0条)

保存