Flask静态文件路由中的变量[url_for('static',filename ='')] [英] Variable in Flask static files routing [url_for('static', filename='')]

查看:2040
本文介绍了Flask静态文件路由中的变量[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 /
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屋!

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