我有一个视图,我正在获取有限制的约会列表..
def HospitalAppointmentView(request, pk, username, hdpk):
todays_appointments = DoctorAppointment.objects.filter(hospital__id=pk, doctor__id=hdpk, appointment_date=today).order_by("-appointment_date")[:5]
return render_to_response('doctor_appointment_list.html', {"todays_appointments": todays_appointments}, context_instance=RequestContext(request))
在我的模板中:
{% for appointment in todays_appointments %}
<h3>{{appointment.doctor.username}}<h3>
<tr>
<td>{{appointment.appointment_date}}</td>
<td>{{appointment.first_name}} {{appointment.middle_name}} {{appointment.last_name}}</td>
<td>{{appointment.user}}</td></tr>
<a href="{% url "all_appointments" appointment.hospital.id appointment.doctor.id%}">
See All</a>
{% endfor %}
正确显示5个约会,除了“全部查看”重复5次,我想将医生的用户名作为标题,并且还要打印5次。
当我点击“全部查看”时,我想重定向到可以看到所有约会的页面。像:
def HospitalAppointmentView(request, pk, username, hdpk):
todays_appointments = DoctorAppointment.objects.filter(hospital__id=pk, doctor__id=hdpk, appointment_date=today).order_by("-appointment_date")
return render_to_response('all_appointment.html', {"todays_appointments": todays_appointments}, context_instance=RequestContext(request))
如果我在for循环之外写“See All”,我就无法访问hospital.id和doctor.id并且在循环内部我得到“全部看见”5次,同样符合{{appointment.doctor.username} }。
如何在不打印的情况下重定向5次,并且网址中需要所有信息并且{{appointment.doctor.username}}被打印一次?
答案 0 :(得分:7)
您可以使用{{forloop.first}}
,第一次迭代就是如此。
喜欢......
{% for appointment in todays_appointments %}
{% if forloop.first %}
<h3>{{appointment.doctor.username}}<h3>
{%endif%}
...
{%endfor%}
答案 1 :(得分:0)
{{todays_appointments.0.doctor.username}}
和你的正常循环。
{% for appointment in todays_appointments %}
....
{% endfor %}
但是罗恩的回答更明智