파이썬에서 루핑에 문제가 있고 변수를 기반으로 JSON이 있습니다. 시도한 것은 통과 된 하나의 변수를 기반으로 한 행의 JSON 데이터 만 인쇄하는 것입니다.파이썬과 플라스크, 루핑 및 변수를 기반으로 데이터 인쇄
을 Heres파이썬 파일에 대한 코드 :
@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)
그리고 이것은 내 artist.html 템플릿입니다 :
{% 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 %}
에 "ARTIST_NAME"변수 인 경우가되는 것이 무엇 메신저 달성하기 위해 노력하고
passed "artist_name"변수와 "date_of_release"변수를 인쇄 할 수 있지만 "artist_name"변수를 기반으로하는 대신 네 개의 레코드를 모두 인쇄합니다. 아무도 이걸 도와 줄 수 있니? 고맙습니다.
아마도이 때문에 : item.artist_name == item.artist_name'? – hjpotter92
item.artist_name == 결과와 같은 다양한 변형을 시도했지만 올바르게 수행하는 방법을 알 수 없습니다. – Animalovsky
try item.artist_name == artist_name –