-1

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.

Animalovsky
  • 43
  • 1
  • 9

2 Answers2

0

my solution is less complicated, and working :)

from flask import Flask,render_template,url_for
import json
app = Flask(__name__)
app.debug = False

@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'}
    ]
    res = ""
    for i in list:
        if i.get('artist_name') == artist_name:
            res = i
    return render_template("artist.html", results=list, artist_name=res)    

if __name__ == '__main__':
    app.run(host='0.0.0.0')

template:

{% if results %}
<ul>

    <li>{{ artist_name["artist_name"] }}</li>
    <li>{{ artist_name["date_of_release"]}}</li>

</ul>
{% endif %}

(artist_name in url is case sensitive)

János Farkas
  • 453
  • 5
  • 14
0

In the comments, H J potter and Markus have it right: however I'ld be very careful over the url encoding of your url and enforcing capitlisation of the artist name: Is "System Of A Down" the same as "System of a Down", and does flask always decode correctly?

{% if results %}
<ul>
{% for item in results if item.artist_name == artist_name %}
    <li>{{ item.artist_name }}</li>
    <li>{{ item.date_of_release}}</li>
{% endfor %}
</ul>
{% endif %}

Have a look at Is a URL allowed to contain a space? TLDR? "Shorter answer: no, you must encode a space; it is correct to encode a space as +, but only in the query string; in the path you must use %20."

I would also go further though and suggest a restructure of the code - try to keep business logic (i.e. deciding not to display or load all the data) in one file, the 'python', and the display logic (deciding to put them on bullet points or on divs) in the other (the 'template')

Community
  • 1
  • 1
Jmons
  • 1,766
  • 17
  • 28