如何在此展示医院?(https://ibb.co/Xkn58yK)
blood/models
from django.db import models
class Patient(models.Model):
patient_id = models.CharField(max_length= 1000, null= True)
first_name = models.CharField(max_length = 50, null = True)
last_name = models.CharField(max_length= 50, null = True)
address = models.CharField(max_length=100, null = True, blank=True)
phone_number = models.CharField(max_length=50, null= True, blank=True)
date_created = models.DateTimeField(auto_now_add = True, null = True, blank=True)
hospitals = models.ManyToManyField(Hospital)
BLOOD_TYPE = (
('O+','O+'),
('O-','O-'),
('A+','A+'),
('A-','A-'),
('B+','B+'),
('B-','B-'),
('AB+','AB+'),
('AB-','AB-'),
)
patient_blood = models.CharField(max_length=5, null = True , choices=BLOOD_TYPE)
def __str__(self):
return '{} {} {} {}' .format(self.first_name, self.last_name, "-", self.patient_id)
class Hospital(models.Model):
hospital = models.CharField(max_length=100, null=True)
address = models.CharField(max_length=100, null = True, blank=True)
phone_number = models.CharField(max_length=50, null= True, blank=True)
def __str__(self):
return self.hospitalviews.py
blood/views
from django.shortcuts import render, redirect
from django.http import HttpResponse
from .forms import *
def PatientPage(request):
patients = Patient.objects.all()
hospitals = Hospital.objects.all()
context = {
'patients':patients,
'hospitals':hospitals,
}
return render(request, 'blood/patients.html', context)我可以询问剩下的,只剩下医院了
{% for id in patients %}
<tr>
<td>{{id.hospitals}}</td>
</tr>
{% endfor %}这是超文本标记语言部分,我列出了其余的部分,但我不能做ManyToManyField。唯一的问题是我如何显示病人选择的医院
我才刚开始学,所以很抱歉
发布于 2020-09-19 23:39:59
您的数据模型似乎不正确,但基于当前模型,这可能是可行的:
{% for p in patients %}
<table>
<tr><td>Date Registered</td><td>Hospital</td><td>Blood Type</td></tr>
{{for h in p.hospitals}}
<tr><td>{{p.date_created}}</td><td>{{h.hospital}}</td><td>{{p.patient_blood}}</td></tr>
{{endfor}}
</table>
{% endfor %}https://stackoverflow.com/questions/63969577
复制相似问题