Flask静态文件路由中的变量[url_for('static',filename ='')] [英] Variable in Flask static files routing [url_for('static', filename='')]
问题描述
我想让用户上传他们的音频文件,我有一个页面,我打算显示所有的歌曲。
我创建了一个模板,结构如下所示:
{歌曲中歌曲的百分比%}
< div class =chart-item>
< div class =chart-position col-md-1>
< h3>#u< / h3>
< / div> <! - 图表位置 - >
< div class =band-logo col-md-2>
< / div> <! - band-logo - >
< div class =band-name-and-autio col-md-9>
< div class =band-name> {{song ['artistName']}} - {{song ['songName']}}< / div> <! - 带名 - >
< div class =audio>
< audio>
< / audio>
< / div> <! - 音频 - >
< / div> <! - band-name-and-autio - >
< div class =clearfix>< / div>
< / div> <! - chart-item - >
{%endfor%}
这里我想制作一个动态的路径来覆盖图片和一个记录,但我不知道在这里正确地写入文件的路径:
< img src ={{ url_for('static',filename ='uploads / users / {{song ['artistName']}} / {{song ['pathToCover']}}')}}>
请解释如何去做。我试图找到烧瓶网页上的解决方案,但现在我没有任何结果。
相信你可以像这样嵌套模板标签。但是你也不应该需要。
< img src ={{url_for('static',filename ='uploads / users /')}} { {song ['artistName']}} / {{song ['pathToCover']}}>
你可以从下面的例子中看到这个原理:
>>> from flask import Flask,url_for
>>> app = Flask(__ name__)
>>>使用app.test_request_context():
... print url_for('static',filename ='uploads / users /')
/ static / uploads / users /
$ c $所以你只需要添加 artistName
, />
, pathToCover
I'm making a simple music app. I want to allow users upload their audio files and I have a page where I'm planning to show all songs. I've created a template, and the structure looks like:
{% for song in songs %}
<div class="chart-item">
<div class="chart-position col-md-1">
<h3>#u</h3>
</div> <!-- chart-position -->
<div class="band-logo col-md-2">
<img src="{{ url_for('static', filename='uploads/users/{{ song['artistName'] }}/{{ song['pathToCover'] }}')}}">
</div> <!-- band-logo -->
<div class="band-name-and-autio col-md-9">
<div class="band-name">{{ song['artistName'] }} - {{ song['songName'] }}</div> <!-- band-name -->
<div class="audio">
<audio>
</audio>
</div> <!-- audio -->
</div> <!-- band-name-and-autio -->
<div class="clearfix"></div>
</div> <!-- chart-item -->
{% endfor %}
Here I want to make a dynamical path to cover image and a record, but I do not know to correctly write the path to the file here:
<img src="{{ url_for('static', filename='uploads/users/{{ song['artistName'] }}/{{ song['pathToCover'] }}')}}">
Please, explain how to do it. I've tried to find the solution on flask web page, but for now I have no any result.
I don't believe you can nest template tags like that. But you also shouldn't need to.
<img src="{{ url_for('static', filename='uploads/users/') }}{{ song['artistName'] }}/{{ song['pathToCover'] }}">
You can see why this works from the following example:
>>> from flask import Flask, url_for
>>> app = Flask(__name__)
>>> with app.test_request_context():
... print url_for('static', filename='uploads/users/')
/static/uploads/users/
So then you just need to add the artistName
, /
, pathToCover
这篇关于Flask静态文件路由中的变量[url_for('static',filename ='')]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!