2015-06-09 113 views
0

查询后的每个结果被我的收藏命名为“患者”在MongoDB中执行:我有这样的代码,结果名为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" } 

我渲染包含表格的模板:

return render_template('patient-record.html', patient_doc=doc) 

在浏览器中,我只显示一个患者,我如何使用jinja语法来查看所有结果。我的意思是我怎么可以做一个循环来显示我病例中每个病人的数据。我试过{% for patient_doc in patient_doc %}但没有结果。

HTML文件命名为患者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> 

任何机构可以帮我解决这个问题?

回答

0

只是不要在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> 
+0

@VictorLean首先感谢您的回答。我想要一些动态的东西,因为我可以添加和删除患者,然后如果我添加例如100位患者,我该如何做100张桌子? – Babel

+0

@Egzona刚刚编辑过这个例子,只需使用'''''和'''''标记。 –

+0

@Egzona你见过更新的代码吗? –