我试图在Django视图中呈现元素。每个诊所对象都有许多特性,但出于美观的原因,我只希望在模板中显示前三个。我试过:
def clinics_index(request):
clinics = Clinic.objects.all()
for clinic in clinics:
speciality = clinic.get_speciality_display
context = {
'clinics' : clinics,
'speciality' : speciality,
}
return render(request, 'guide/clinic/clinic_directory.html', context)
现在呈现了专业字段的人类可读名称(这是模型中的一个选择字段)。但是,我不能像这样使用减法只得到3个元素:
speciality = clinic.get_speciality_display[:3]
当我得到以下错误时:
TypeError at /guide/clinics/
'method' object is not subscriptable
我如何渲染它?
编辑:
这是Clinic模型:
class Clinic(models.Model):
name = models.CharField(max_length=75, blank=True, null=True)
speciality = MultiSelectField(choices=Speciality.choices, max_length=100, blank=True, null=True)
city = models.CharField(max_length=20, choices=Cities.choices, blank=True, null=True)
ward = models.CharField(max_length=20, choices=Wards.choices, blank=True, null=True)
full_address = models.CharField(max_length=100, blank=True, null=True)
maps_link = models.CharField(max_length=75, blank=True, null=True)
train_access = models.CharField(max_length=50, blank=True, null=True)
bus_access = models.CharField(max_length=50, blank=True, null=True)
parking = models.CharField(_('Parking availability'), max_length=75, blank=True, null=True)
phone_number = models.CharField(max_length=20, blank=True, null=True)
english_support = models.BooleanField(default=False, blank=True, null=True)
holiday_availability = models.BooleanField(_('Availability on weekends/holidays'), default=False, blank=True, null=True)
slug = models.SlugField(blank=True, null=True)
def __str__(self):
return self.name
def get_absolute_url(self):
return reverse('guide:clinic_detail', kwargs={"slug" : self.slug})
和模板片段:
<tbody>
{% for clinic in clinics %}
<tr>
<td>{{clinic.name}}</td>
<td>{{clinic.city}}</td>
<td>{{clinic.ward}}</td>
<td>{{speciality}}</td>
<td><a href="{{clinic.get_absolute_url}}">More...</a></td>
</tr>
{% endfor %}
</tbody>
编辑:
下面的代码呈现了我想要的前3个人类可读的元素:
clinics = Clinic.objects.all()
for clinic in clinics:
speciality = ','.join(clinic.get_speciality_display().split(',')[:3])
然而,我正在努力用相应的实例正确地呈现它。这段代码:
fff = [{'name': i.name, 'speciality': ','.join(i.speciality[:3])} for i in Clinic.objects.all()]
呈现非人类可读的名称。如何连接两者(并为每个实例显示city
和ward
字段)?
我假设在一个循环中您希望收集所有的数据。为此,您需要将它们保存到一个列表中。但这是多余的,只是将clinics
传递给字典并迭代模板中的所有值。此外,对于链接,我使用clinic.slug
而不是clinic.get_absolute_url
,因为模型已经通过get_absolute_url
方法返回生成的url。
views.py
def clinics_index(request):
clinics = Clinic.objects.all()[:3]
return render(request, 'guide/clinic/clinic_directory.html', {'context': clinics})
模板{% for clinic in context %}
<p>{{ clinic }}</p>
<tr>
<td>{{ clinic.name }}</td>
<td>{{ clinic.city }}</td>
<td>{{ clinic.ward }}</td>
<td>{{ clinic.speciality }}</td>
<td><a href="{{ clinic.slug }}">More...</a></td>
</tr>
{% endfor %}
</tbody>
更新05.11.2022要获取clinic.get_speciality_display()值,需要使用括号调用该方法。当我输出值类型时,我得到一个字符串。因此,为了获取前三个元素,我将字符串转换为列表,选择所需的数字,并再次将其转换为字符串。
所以你可以选择前三条记录:
clinics = Clinic.objects.all()
for clinic in clinics:
speciality = ','.join(clinic.get_speciality_display().split(',')[:3])
所有的代码:views.py
def clinics_index(request):
#fff = [{'name': i.name, 'speciality': i.speciality[:3]} for i in Clinic.objects.all()]#if you need to display 'speciality' as a list
fff = [{'name': i.name, 'speciality': ','.join(i.speciality[:3])} for i in Clinic.objects.all()]
return render(request, 'guide/clinic/clinic_directory.html', {'context': fff})
模板<tbody>
{% for a in context %}
<tr>
<p><td>{{ a.name }}</td></p>
<p><td>{{ a.speciality }}</td></p>
</tr>
{% endfor %}
</tbody>
如果这不是你需要的。显示数据的样子和您想要看到的内容。