getting template syntax error in django template

2024/10/15 18:30:42

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]'
Answer

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)
https://en.xdnf.cn/q/117802.html

Related Q&A

Python threading:Is it okay to read/write multiple mutually exclusive parts of a file concurrently?

I know we can guarantee correctness either by locking or using a specialized thread whose sole job is to read/write and communicate with it through queue. But this approach seems logically ok, so I wan…

Theano Cost Function, TypeError: Unknown parameter type: class numpy.ndarray

Im new to Theano, just learning it. I have a ANN in python that Im implementing in Theano as learning process. Im using Spyder.And Theano throws out an error: TypeError: Unknown parameter type: class n…

Bar plotting grouped Pandas

I have a question regarding plotting grouped DataFrame data.The data looks like:data =index taste food0 good cheese 1 bad tomato 2 worse tomato 3 worse …

Nginx+bottle+uwsgi Server returning 404 on every request

I have setup an Nginx server with following configuration:server {listen 8080;server_name localhost;location / {include uwsgi_params;uwsgi_pass unix:/tmp/uwsgi.notesapi.socket;uwsgi_param UWSGI_PYHOME …

Checking multiple for items in a for loop python

Ive written a code to tell the user that the their brackets are not balanced.I can exactly tell where my code is going wrong.Once it comes across the first situation of brackets, it does not continue t…

Can I export pandas DataFrame to Excel stripping tzinfo?

I have a timezone aware TimeSeries in pandas 0.10.1. I want to export to Excel, but the timezone prevents the date from being recognized as a date in Excel.In [40]: resultado Out[40]: fecha_hora 2013-…

Converting kwargs into variables?

How do I convert kwargs objects into local variables? I am a math teacher and I want to write a helper function where I can use JS-style template strings to write problems.I want to do something like …

Maya Python: Unbound Method due to Reload()

I have two files that import the same object tracking method from a third file. It works something like thisfile TrackingMethodclass Tracker(object):def __init__(self,nodeName=None,dag=None,obj=None):#…

How to append to a table in BigQuery using Python BigQuery API

Ive been able to append/create a table from a Pandas dataframe using the pandas-gbq package. In particular using the to_gbq method. However, When I want to check the table using the BigQuery web UI I s…

Splitting values out of a CSV Reader Python

Here is my current code a_reader = None a_reader = open(data.csv, rU) a_csv_reader = csv.reader(a_reader)for row in a_csv_reader:print row a_reader.close()count = 0 sum = 0.0 a_reader = open(…