Django打印循环值仅打开

2024-09-30 08:20:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个视图,我可以从中获取带有限制的约会列表。。在

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))

在我的模板中:

^{pr2}$

它显示的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”,我就不能访问医院.id以及医生.id在循环里,我得到了“看所有”5次,同样的{{预约医生用户名}}. 在

如何在不打印5次的情况下重定向url和{{预约医生用户名}}印一次?在


Tags: iddaterequestdefusername用户名appointment约会
2条回答
{{todays_appointments.0.doctor.username}}

还有你的正常循环。在

^{pr2}$

但罗汉的回答更明智

您可以使用{{forloop.first}}for,对于第一次迭代,它将是真的。 就像。。。在

{% for appointment in todays_appointments %}

    {% if forloop.first %}
        <h3>{{appointment.doctor.username}}<h3>
    {%endif%}

    ...
{%endfor%}

相关问题 更多 >

    热门问题