I have a problem with looping in python and JSON based on a variable. What Im trying to do is to print just one row of JSON data based on one variable that is passed through.
Heres the code for the python file:
@app.route('/<artist_name>/')
def artist(artist_name):
list = [
{'artist_name': 'Nirvana', 'album_name': 'Nevermind', 'date_of_release': '1993', 'img': 'https://upload.wikimedia.org/wikipedia/en/b/b7/NirvanaNevermindalbumcover.jpg'},
{'artist_name': 'Eminem', 'album_name': 'Marshal Mathers LP', 'date_of_release': '2000', 'img': 'http://e.snmc.io/lk/f/l/6b09725acea3aefbafbf503a76885d0c/1612455.jpg'},
{'artist_name': 'System of a Down', 'album_name': 'Toxicity', 'date_of_release': '2001', 'img': 'http://loudwire.com/files/2015/09/System-of-a-Down-Toxicity.png'},
{'artist_name': 'Korn', 'album_name': 'Life is Peachy', 'date_of_release': '1996', 'img': 'http://loudwire.com/files/2014/01/Life-is-Peachy.jpg'}
]
return render_template("artist.html", results=list, artist_name=artist_name)
And this is my artist.html template:
{% if results %}
<ul>
{% for item in results if item.artist_name == item.artist_name %}
<li>{{ item.artist_name }}</li>
<li>{{ item.date_of_release}}</li>
{% endfor %}
</ul>
{% endif %}
What im trying to achieve is that when an "artist_name" variable is being passed I will be able to print "artist_name" variable and "date_of_release" variable but instead it prints all four records instead of one based on the variable "artist_name". Can anybody help me with this? Thank you.