data = cur.fetchone()
this only return 1 which mean you do not have to loop
try -> delete “{% for row in data %} ” ?
Solution 1 :
Solution 2 :
Here is the answer to the above problem. Need to add this session[‘data’]=data
@app.route('/dashboard', methods=['GET', 'POST'])
#@is_logged_in
def dashboard():
form = GenreForm(request.form)
if request.method == 'POST':
genre = request.form['genre']
cur = mysql.connection.cursor()
result = cur.execute("""SELECT * FROM music_src WHERE type_of_music = %s"""(genre,))
if result > 0:
data = cur.fetchall()
**session['data'] = data**
return redirect(url_for('search', data=data))
else:
return redirect(url_for('dashboard'))
cur.close()
return redirect(url_for('search', data=data))
Problem :
This is my app.py file mysql query retrives to data normally
@app.route('/dashboard', methods=['GET', 'POST'])
#@is_logged_in
def dashboard():
form = GenreForm(request.form)
if request.method == 'POST':
genre = request.form['genre']
cur = mysql.connection.cursor()
result = cur.execute("""SELECT * FROM music_src WHERE type_of_music = %s"""(genre,))
if result > 0:
data = cur.fetchone()
else:
return redirect(url_for('dashboard'))
cur.close()
return redirect(url_for('search', data=data))
Here is my search.html code
{% extends 'layout.html' %}
{% block body %}
<table border="1" bordercolor="white" cellpadding="15" cellspacing="35" style="font-family:Georgia, Garamond, Serif;color:white;font-style:italic;">
<thead>
<tr>
<th>ID</th>
<th>Title</th>
<th>Type_of_music</th>
</tr>
</thead>
<tbody>
{% for row in data %}
<tr>
<td>{row[0]}</td>
<td>{row[1]}</td>
<td>{row[2]}</td>
</tr>
{% endfor %}
</tbody>
</table>
{% endblock %}
When i debugged mysql query passes data like below. Is this the reason why its not printing? If so how can i print this in table format of ID Title and Type_of_music
data={'id':+28,+'title':+'Once+upon+a+time+–+James+Bond',+'type_of_music':+'sad'}
Comments
Comment posted by Raviteja V
Even cur.fetchall() doesn’t print in the html. So I tried to replace it with cur.fetchone() to debug it, still not working.