Python and flask, looping and printing data based on a variable
<p>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.</p>

<p>Heres the code for the python file:</p>

<pre><code>@app.route('/&lt;artist_name&gt;/')
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)    
</code></pre>

<p>And this is my artist.html template:</p>

<pre><code>{% if results %}
&lt;ul&gt;
{% for item in results if item.artist_name == item.artist_name %}
    &lt;li&gt;{{ item.artist_name }}&lt;/li&gt;
    &lt;li&gt;{{ item.date_of_release}}&lt;/li&gt;
{% endfor %}
&lt;/ul&gt;
{% endif %}
</code></pre>

<p>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.</p>

<p>my solution is less complicated, and working :)</p>

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

@app.route('/&lt;artist_name&gt;/')
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')
</code></pre>

<p>template:</p>

<pre><code>{% if results %}
&lt;ul&gt;

    &lt;li&gt;{{ artist_name["artist_name"] }}&lt;/li&gt;
    &lt;li&gt;{{ artist_name["date_of_release"]}}&lt;/li&gt;

&lt;/ul&gt;
{% endif %}
</code></pre>

<p>(artist_name in url is case sensitive)</p>

python json loops variables flask