带有变量的Python Flask url_for(静态) [英] Python Flask url_for (static) with variable

查看:57
本文介绍了带有变量的Python Flask url_for(静态)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将 Flask 与Python配合使用,并希望插入图像.
使用以下命令可以正常工作:

i'm using Flask with Python and want to insert an Image.
That works fine with following command:

{{ url_for('static', filename='path/to/img.jpg') }}

但是现在我想在循环中插入图像(就像文章一样),并且需要像这样的变量之外的路径:

But now i want to insert an image in a loop (like for articles) and need the path out of a variable like:

{{ article.header_img_path }}

如何将变量路径插入

{{url_for('static',filename ='path/to/img.jpg')}}

{{ url_for('static', filename='path/to/img.jpg') }}

命令?
像这样的东西:

command?
Something like that:

{{ url_for('static', filename=' {{ article.header_img_path }} ') }}


有没有更好的方法可以做到这一点,或者只是我的语法被破坏了?


Is there a better way to do that or is just my syntax corrupt?


谢谢您的指教!


Thanks for your advise!

推荐答案

该变量应传递给 filename ,且不要使用引号或花括号.

The variable should be passed to filename without quotes or braces.

{{ url_for('static', filename=article.header_img_path) }}

这篇关于带有变量的Python Flask url_for(静态)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆