0

my code in view :

tracks = client.get('/tracks', order='hotness', limit=4)   
artwork_url=[]
for track in tracks:
    artwork_url.append(str(track.artwork_url).replace("large", "t300x300"))        


val={"tracks":tracks,"artwork_url":artwork_url}    
return render_to_response('music/tracks.html',val)

in .html

{% for track in tracks %} 
<li>
    <div class="genre-image">
        <img src="{{   artwork_url[forloop.counter] }}">
    </div>
{% endfor %} 

Error:

Exception Type: TemplateSyntaxError 
Exception Value:  Could not parse the remainder: '[forloop.counter]' from 'artwork_url[forloop.counter]'
1
  • Are the elements of tracks immutable? If not, then just append to track variable instead of artwork_url. Commented Jun 23, 2013 at 6:44

1 Answer 1

4

Since your artwork_url is a list, the reasonable way would be to access it like this:

artwork_url.forloop.counter

but it won't work. The Django template language isn't that advanced unfortunately.

You should access it like this.

{% for track in tracks %} 
<li><div class="genre-image">
<img src="{{ track.artwork_url }}">
</div>
{% endfor %}

But that requires that the tracks are mutable and require you to change it in the backend.

So if you're not able to modify the track you'd have to implement a custom template filter something like this

{{ track.artwork_url|myFormatter:'t300' }}

A very small and simple formatter:

@register.filter(name='myDate')
def myFormatter(value, arg):
    if arg == 't300':
        arg = 't300x300'
    return str(value).replace("large", arg)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.