使用 Jinja 查看查询的每个结果,python
View each result of a query with Jinja , python
在 mongodb 中名为 'patients' 的集合中执行查询后:我得到的结果代码为 patient_doc
:
{ "_id" : "5576ee2e1a6d6d0d9b879095", "date_of_birth" : "29/06/2015", "diagnosis" : "Mamografia", "doctor_id" : "554b6e791a6d6d34cc3272f7", "name" : "Xona", "sex" : "Female", "surname" : "Krasniqi" }
{ "_id" : "5576ee2e1a6d6d0d9b879095", "date_of_birth" : "29/06/2015", "diagnosis" : "Ekografia", "doctor_id" : "554b6e791a6d6d34cc3272f7", "name" : "John", "sex" : "Male", "surname" : "Esma" }
我渲染了一个包含 table:
的模板
return render_template('patient-record.html', patient_doc=doc)
在浏览器中我只显示了一个病人,我如何使用 jinja 语法来查看所有结果。我的意思是我怎样才能做一个循环来显示我案例中每个病人的数据。我尝试了 {% for patient_doc in patient_doc %}
但没有结果。
Html 名为 patient-record.html 的文件包含此代码:
<table class="patient-view-table">
<tr>
<td class="property-name-col">Name:</td>
<td class="property-value-col">{{ patient_doc.name }}</td>
</tr>
<tr>
<td class="property-name-col">Surname:</td>
<td class="property-value-col">{{ patient_doc.surname }}</td>
</tr>
<tr>
<td class="property-name-col">Sex:</td>
<td class="property-value-col">{{ patient_doc.sex }}</td>
</tr>
<tr>
<td class="property-name-col">Date of birth:</td>
<td class="property-value-col">{{patient_doc.date_of_birth}}
</td>
</tr>
<tr>
<td class="property-name-col">Diagnosis:</td>
<td class="property-value-col">{{ patient_doc.diagnosis }}</td>
</tr>
</table>
任何人都可以帮我解决这个问题吗?
只是不要在 for 循环中重复变量名 patient_doc
。
这样做:
{% for patient in patient_doc %}
示例:
<table class="patient-view-table">
<thead>
<tr>
<th>Name</th>
<th>Surname</th>
<th>Sex</th>
<th>Date of birth</th>
<th>Diagnosis</th>
</tr>
</thead>
<tbody>
{% for patient in patient_doc %}
<tr>
<td class="property-value-col">{{ patient.name }}</td>
<td class="property-value-col">{{ patient.surname }}</td>
<td class="property-value-col">{{ patient.sex }}</td>
<td class="property-value-col">{{ patient.date_of_birth }}</td>
<td class="property-value-col">{{ patient.diagnosis }}</td>
</tr>
{% endfor %}
</tbody>
</table>
如果结果是一个列表,我认为变量的名称应该是 patients_doc
return render_template('patient-record.html', patients_doc=doc)
示例:
<table class="patient-view-table">
<thead>
<tr>
<th>Name</th>
<th>Surname</th>
<th>Sex</th>
<th>Date of birth</th>
<th>Diagnosis</th>
</tr>
</thead>
<tbody>
{% for patient in patients_doc %}
<tr>
<td class="property-value-col">{{ patient.name }}</td>
<td class="property-value-col">{{ patient.surname }}</td>
<td class="property-value-col">{{ patient.sex }}</td>
<td class="property-value-col">{{ patient.date_of_birth }}</td>
<td class="property-value-col">{{ patient.diagnosis }}</td>
</tr>
{% endfor %}
</tbody>
</table>
在 mongodb 中名为 'patients' 的集合中执行查询后:我得到的结果代码为 patient_doc
:
{ "_id" : "5576ee2e1a6d6d0d9b879095", "date_of_birth" : "29/06/2015", "diagnosis" : "Mamografia", "doctor_id" : "554b6e791a6d6d34cc3272f7", "name" : "Xona", "sex" : "Female", "surname" : "Krasniqi" }
{ "_id" : "5576ee2e1a6d6d0d9b879095", "date_of_birth" : "29/06/2015", "diagnosis" : "Ekografia", "doctor_id" : "554b6e791a6d6d34cc3272f7", "name" : "John", "sex" : "Male", "surname" : "Esma" }
我渲染了一个包含 table:
的模板return render_template('patient-record.html', patient_doc=doc)
在浏览器中我只显示了一个病人,我如何使用 jinja 语法来查看所有结果。我的意思是我怎样才能做一个循环来显示我案例中每个病人的数据。我尝试了 {% for patient_doc in patient_doc %}
但没有结果。
Html 名为 patient-record.html 的文件包含此代码:
<table class="patient-view-table">
<tr>
<td class="property-name-col">Name:</td>
<td class="property-value-col">{{ patient_doc.name }}</td>
</tr>
<tr>
<td class="property-name-col">Surname:</td>
<td class="property-value-col">{{ patient_doc.surname }}</td>
</tr>
<tr>
<td class="property-name-col">Sex:</td>
<td class="property-value-col">{{ patient_doc.sex }}</td>
</tr>
<tr>
<td class="property-name-col">Date of birth:</td>
<td class="property-value-col">{{patient_doc.date_of_birth}}
</td>
</tr>
<tr>
<td class="property-name-col">Diagnosis:</td>
<td class="property-value-col">{{ patient_doc.diagnosis }}</td>
</tr>
</table>
任何人都可以帮我解决这个问题吗?
只是不要在 for 循环中重复变量名 patient_doc
。
这样做:
{% for patient in patient_doc %}
示例:
<table class="patient-view-table">
<thead>
<tr>
<th>Name</th>
<th>Surname</th>
<th>Sex</th>
<th>Date of birth</th>
<th>Diagnosis</th>
</tr>
</thead>
<tbody>
{% for patient in patient_doc %}
<tr>
<td class="property-value-col">{{ patient.name }}</td>
<td class="property-value-col">{{ patient.surname }}</td>
<td class="property-value-col">{{ patient.sex }}</td>
<td class="property-value-col">{{ patient.date_of_birth }}</td>
<td class="property-value-col">{{ patient.diagnosis }}</td>
</tr>
{% endfor %}
</tbody>
</table>
如果结果是一个列表,我认为变量的名称应该是 patients_doc
return render_template('patient-record.html', patients_doc=doc)
示例:
<table class="patient-view-table">
<thead>
<tr>
<th>Name</th>
<th>Surname</th>
<th>Sex</th>
<th>Date of birth</th>
<th>Diagnosis</th>
</tr>
</thead>
<tbody>
{% for patient in patients_doc %}
<tr>
<td class="property-value-col">{{ patient.name }}</td>
<td class="property-value-col">{{ patient.surname }}</td>
<td class="property-value-col">{{ patient.sex }}</td>
<td class="property-value-col">{{ patient.date_of_birth }}</td>
<td class="property-value-col">{{ patient.diagnosis }}</td>
</tr>
{% endfor %}
</tbody>
</table>