Reputation: 67
I want to select data from database table in MySQL. It is procedure in Python
@app.route('/formworkers.html')
def formworkers():
cursor = mysql.connection.cursor()
cur = cursor.execute("SELECT ID_worker, surname FROM workers")
return render_template('formworkers.html', workers0=cursor.fetchall())
And template in Jinja2
{% extends "layout.html" %}
{% block body %}
{% if session.logged_in %}
<form action="" method=post class=add-worker >
<dl>
<dt>Surname:
<dd><select name="worker0">
{% for ID_pats, surname in workers0 %}
<option value="{{ ID_worker }}">{{ surname }}</option>
{% endfor %}
</select>
<br><br>
<dd><input type=submit value="Submit">
</dl>
</form>
{% endif %}
{% endblock %}
But the dropdown list does not contain data (for example [Ivanov, Petrov, Sidorov]) from the database, it contain values [surname, surname, surname]. Thank you.
Upvotes: 1
Views: 7365
Reputation: 13327
The query outputs a result as a list of dictionaries, so when you unpack it in the for
loop, you get only a list of keys "ID_worker"
and "surname"
.
You can change the template like below to access the values :
{% for worker in workers0 %}
<option value="{{ worker.ID_worker }}">{{ worker.surname }}</option>
{% endfor %}
Upvotes: 2