Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Variable in Flask static files routing [url_for('static', filename='')] [duplicate]

Tags:

python

flask

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.

like image 433
Nodari Lipartiya Avatar asked Oct 22 '13 07:10

Nodari Lipartiya


People also ask

What does url_for do in Flask?

url_for in Flask is used for creating a URL to prevent the overhead of having to change URLs throughout an application (including in templates). Without url_for , if there is a change in the root URL of your app then you have to change it in every page where the link is present.

How do I use a static file in Flask?

To reference the static files using the url_for() function, pass in the name of the directory – static in this case – and the keyword argument of filename= followed by your static file name. Flask automatically creates a static view that serves static files from a folder named static in your application's directory.

How do I change my static folder in Flask?

It turns out the official Flask docs specify that the static url path is configurable using the 'static_url_path' parameter to the Flask application initializer. to include the static_url_path set to something useful like /public will do the trick. And voila, problem solved!


2 Answers

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

like image 193
aychedee Avatar answered Oct 17 '22 15:10

aychedee


I think you can do

<img src="{{ url_for('static', filename='uploads/users/'+ song['artistName']+'/'+ song['pathToCover'])}}">

It works for me :)

like image 30
Bones and Teeth Avatar answered Oct 17 '22 14:10

Bones and Teeth