Проблема в том, как вы передаете данные в шаблон, используя render_template из документации:
Визуализирует шаблон из папки шаблонов с заданным контекстом.
Параметры: template_name_or_list - имя шаблона, который будет
визуализированный или итерируемый с именами шаблонов, из которых существует первый
будет представлен контекст - переменные, которые должны быть доступны в
контекст шаблона.
context
- это не что иное, как переменные, которые вы хотите передать в качестве аргументов ключевых слов. Следуйте примеру, сначала файл python app.py
:
#!/usr/bin/python3
from flask import Flask, render_template
app = Flask(__name__)
@app.route("/tuple")
def jpg_to_pdf():
results = [(1, 'Dummy1', 'dummy1', 1), (2, 'Dummy2', 'dummy2', 2), (3, 'Dummy3', 'dummy3', 3)]
return render_template('index.html', data=results)
и шаблон Jinja:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Tuple</title>
</head>
<body>
<table style="width: 100%">
{% for element in data %}
<tr>
<th>{{ element[0] }}</th>
<th>{{ element[1] }}</th>
<th>{{ element[2] }}</th>
<th>{{ element[3] }}</th>
</tr>
{% endfor %}
</table>
</body>
</html>
Сочетание этих файлов отображает следующий код html
:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Tuple</title>
</head>
<body>
<table style="width: 100%">
<tr>
<th>1</th>
<th>Dummy1</th>
<th>dummy1</th>
<th>1</th>
</tr>
<tr>
<th>2</th>
<th>Dummy2</th>
<th>dummy2</th>
<th>2</th>
</tr>
<tr>
<th>3</th>
<th>Dummy3</th>
<th>dummy3</th>
<th>3</th>
</tr>
</table>
</body>
</html>
Подробнее о шаблонах рендеринга можно узнать здесь .