Jinja 中的嵌套循环
Nested loop in Jinja
我的数据是 list/tuple 个键和 list/tuple 个值,我需要将其循环并并排显示为 table 中的问题 - 答案.
数据为questions_page_data。
{
(
'Organisation name', 'Organisation address', 'Type of organisation',
'Have you delivered projects like this before?',
'Upload evidence to support your answer',
'Your Accountant', 'Responsible person',
'Do you have endorsements to support your application?',
'Who is endorsing your application?',
'Upload evidence to support your answer'
):
(
'sa', 'sas, asa, as, as', 'Limited Company', True, None, 'as', 'as', False,
'weqwe', None
)
}
Jinja 代码:
<table>
{% for questions,answers in questions_page_data.items()%}
<tbody class="table__body">
{% if questions %}
{% for question in questions %}
{% if answers %}
{% for answer in answers %}
<tr class="table__row">
<td class="table__cell">
{{question }}
</td>
<td class="table__cell">
{{answer}}
</td>
</tr>
{%endfor%}
{%endif%}
{%endfor%}
{%endif%}
</tbody>
{% endfor %}
</table>
我知道 Jinja 代码根本不正确 :)。它应该是这样的。请指教
Question Answer
Organisation name sa
在您的 python 代码中尝试类似的操作:
@app.route('/')
def a_route():
questions_page_data = {
(
'Organisation name', 'Organisation address', 'Type of organisation',
'Have you delivered projects like this before?',
'Upload evidence to support your answer',
'Your Accountant', 'Responsible person',
'Do you have endorsements to support your application?',
'Who is endorsing your application?',
'Upload evidence to support your answer'
):
(
'sa', 'sas, asa, as, as', 'Limited Company', True, None, 'as', 'as', False,
'weqwe', None
)
}
return render_template('a_html_file.html', questions_page_data=questions_page_data, zip=zip)
然后在您的 HTML 模板中,您可以使用 zip
一次迭代您的两个列表:
<table>
{% for questions, answers in questions_page_data.items() %}
<tbody class="table__body">
{% if questions %}
{% for question, answer in zip(questions,answers) %}
<tr class="table__row">
<td class="table__cell">
{{ question }}
</td>
<td class="table__cell">
{{ answer }}
</td>
</tr>
{% endfor %}
{% endif %}
</tbody>
{% endfor %}
</table>
最后,我的结果是这样的:
我的数据是 list/tuple 个键和 list/tuple 个值,我需要将其循环并并排显示为 table 中的问题 - 答案.
数据为questions_page_data。
{
(
'Organisation name', 'Organisation address', 'Type of organisation',
'Have you delivered projects like this before?',
'Upload evidence to support your answer',
'Your Accountant', 'Responsible person',
'Do you have endorsements to support your application?',
'Who is endorsing your application?',
'Upload evidence to support your answer'
):
(
'sa', 'sas, asa, as, as', 'Limited Company', True, None, 'as', 'as', False,
'weqwe', None
)
}
Jinja 代码:
<table>
{% for questions,answers in questions_page_data.items()%}
<tbody class="table__body">
{% if questions %}
{% for question in questions %}
{% if answers %}
{% for answer in answers %}
<tr class="table__row">
<td class="table__cell">
{{question }}
</td>
<td class="table__cell">
{{answer}}
</td>
</tr>
{%endfor%}
{%endif%}
{%endfor%}
{%endif%}
</tbody>
{% endfor %}
</table>
我知道 Jinja 代码根本不正确 :)。它应该是这样的。请指教
Question Answer
Organisation name sa
在您的 python 代码中尝试类似的操作:
@app.route('/')
def a_route():
questions_page_data = {
(
'Organisation name', 'Organisation address', 'Type of organisation',
'Have you delivered projects like this before?',
'Upload evidence to support your answer',
'Your Accountant', 'Responsible person',
'Do you have endorsements to support your application?',
'Who is endorsing your application?',
'Upload evidence to support your answer'
):
(
'sa', 'sas, asa, as, as', 'Limited Company', True, None, 'as', 'as', False,
'weqwe', None
)
}
return render_template('a_html_file.html', questions_page_data=questions_page_data, zip=zip)
然后在您的 HTML 模板中,您可以使用 zip
一次迭代您的两个列表:
<table>
{% for questions, answers in questions_page_data.items() %}
<tbody class="table__body">
{% if questions %}
{% for question, answer in zip(questions,answers) %}
<tr class="table__row">
<td class="table__cell">
{{ question }}
</td>
<td class="table__cell">
{{ answer }}
</td>
</tr>
{% endfor %}
{% endif %}
</tbody>
{% endfor %}
</table>
最后,我的结果是这样的: