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.
{{workers0}}in your template to check what is sent by the view ?<option value="{{ workers0.ID_pats }}">{{ workers0.surname }}</option>, but dropdown list is empty, but have 3 empty elementsfor{{workers0}}?<dd><select name="worker0">, only to see what it contains exactly({'ID_worker': 1L, 'surname': u'\u0418\u0432\u0430\u043d\u043e\u0432'}, {'ID_worker': 2L, 'surname': u'\u041f\u0435\u0442\u0440\u043e\u0432'}, {'ID_worker': 3L, 'surname': u'\u0421\u0438\u0434\u043e\u0440\u043e\u0432'})