带有变量的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) }}