目前我正在研究web应用程序的历史页面。 App(写在Flask上)在sqlite中保存历史记录,并通过SQLAlchemy与db一起工作。 它看起来如下:
Jinja2模板中的模态窗口。 flask,flask
我已经用下一个代码做到了:
{% extends "_base.html" %}
{% block content %}
<div id="div1" class="col-md-3">
<p><a href='/' class='btn btn-primary btn-block' role='button'><span class='glyphicon glyphicon-chevron-left'></span> Back</a></p>
</div>
<div class="bs-example col-md-12">
<br>
<table class="table table-hover">
<thead>
<tr>
<th>Task ID</th>
<th>Date</th>
<th>Host</th>
<th>User</th>
<th>Playbook</th>
<th>Log</th>
</tr>
</thead>
<tbody>
{% for history in histories %}
<tr>
<td>{{ history.task_id }}</td>
<td>{{ history.date.strftime("%d/%m/%y %H:%M") }} UTC</td>
<td>{{ history.hostname }}</td>
<td>{{ history.username }}</td>
<td>{{ history.playbook }}</td>
<td><button type="button" class="btn btn-info btn-sm" data-toggle="modal" data-target="#myOutput{{ history.task_id }}">Output</button></td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
{% for history in histories %}
<!-- Modal -->
<div class="modal fade" id="myOutput{{ history.task_id }}" role="dialog">
<div class="modal-dialog modal-lg">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Task Output</h4>
</div>
<div class="modal-body">
<p>{{ history.output }}</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
{% endfor %}
{% endblock %}
按钮中的data-target
和模态窗口中的id
是根据task_id
值动态生成的。 不知道这是否是最好的方法,但至少它有效。
秘密是使用Jinja2来识别您何时"调用"模态以及模态本身。
因此,在"调用"上,而不仅仅是data-target="#myOutput",而是在末尾添加Jinja2->data-target="#myOutput{{history。task_id}}"
在模态中,用相同的名称标识,而不是:id="myOutput",再次在末尾添加Jinja2->id="myOutput{{history。task_id}}"