django print循环值只有一次 [英] django print loop value only once

查看:163
本文介绍了django print循环值只有一次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  def HospitalAppointmentView(request,pk,用户名,hdpk):
todays_appointments = DoctorAppointment.objects.filter(hospital__id = pk,doctor__id = hdpk,appointment_date = today).order_by( - 约定日期)[:5]

返回render_to_response('doctor_appointment_list.html',{todays_appointments:todays_appointments},context_instance = RequestContext(request))

在我的模板中:

  {%todays_appointments%} 
< h3> {{约会。 doctor.username}}< H3>
< tr>
< td> {{约会/任务日期}}< / td>
< td> {{约会最初的名字}}& nbsp; {{约会的中间名}}& nbsp; {{appointment.last_name}}< / TD>
< td> {{约会用户}}< / td>< / tr>
< a href ={%urlall_appointmentsappointment.hospital.id appointment.doctor.id%}>
查看全部< / a>

{%endfor%}

它显示正确的5个约会,查看全部重复5次,我想以医生的用户名作为标题,并将其打印5次。



当我点击查看全部我想重定向到可以看到所有约会的页面。喜欢:

  def HospitalAppointmentView(request,pk,username,hdpk):
todays_appointments = DoctorAppointment.objects.filter $ _
$ b返回render_to_response('all_appointment.html',{todays_appointments:todays_appointments},context_instance = RequestContext(请求(request_id = pk,doctor__id = hdpk,约会日期=今天) ))

如果我在for循环之外写See All,我无法访问hospital.id doctor.id和里面的循环我得到查看全部5次,同样的{{约会.doctor.username}}。



如果不打印5次,并且打印一次{{约会.doctor.username}}所需的所有信息,我如何重定向?

解决方案

您可以使用 {{forloop.first}} 第一次迭代
喜欢...

  {%在约会在todays_appointments%} 

{%如果forloop.first%}
< h3> {{约会.doctor.username}}< h3>
{%endif%}

...
{%endfor%}


I have a view where I am getting the list of appointments with limit..

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

In my template:

{% for appointment in todays_appointments %}
    <h3>{{appointment.doctor.username}}<h3>
    <tr>
    <td>{{appointment.appointment_date}}</td>
    <td>{{appointment.first_name}} &nbsp;{{appointment.middle_name}} &nbsp; {{appointment.last_name}}</td>
    <td>{{appointment.user}}</td></tr>
    <a href="{% url "all_appointments" appointment.hospital.id appointment.doctor.id%}">
    See All</a>

{% endfor %}

Its showing the 5 appointments correctly except "See All" is repeated 5 times and I want to make doctor's username as title and its also being printed 5 times.

When I click "See All" I want to redirect to the page where all appointments can be seen. Like:

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

If I write "See All" outside the for loop I cant access the hospital.id and doctor.id and inside the loop I m getting "See All " 5 times and same goes with {{appointment.doctor.username}}.

How can I redirect without being printed 5 times with all information needed in the url and {{appointment.doctor.username}} being printed once?

解决方案

You can use {{forloop.first}} for, it will be true for 1st iteration. Like ...

{% for appointment in todays_appointments %}

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

    ...
{%endfor%}

这篇关于django print循环值只有一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆