Reputation: 619
After a query is executed in my collection named 'patients' in mongodb: I have this code as a result that is named 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" }
I render a template that contains a table:
return render_template('patient-record.html', patient_doc=doc)
In the browser i have only one patient displayed , how can i use jinja syntax to view all results. I mean how can I do a loop that will display data for each patient in my case. I tried {% for patient_doc in patient_doc %}
but no result.
Html file named patient-record.html contain this code:
<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>
Can any body help me to solve this?
Upvotes: 0
Views: 1496
Reputation: 65
Just don't duplicate the variable name patient_doc
in the for loop.
Do like this:
{% for patient in patient_doc %}
Example:
<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>
And if the result is a list i think the name of the variable should be patients_doc
return render_template('patient-record.html', patients_doc=doc)
Example:
<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>
Upvotes: 1