python - django print loop value only once -


i have view getting list of appointments 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 template:

{% 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 5 appointments correctly except "see all" repeated 5 times , want make doctor's username title , being printed 5 times.

when click "see all" want redirect page appointments can 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 write "see all" outside loop cant access hospital.id , doctor.id , inside loop m getting "see " 5 times , same goes {{appointment.doctor.username}}.

how can redirect without being printed 5 times information needed in url , {{appointment.doctor.username}} being printed once?

you can use {{forloop.first}} for, true 1st iteration. ...

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

Comments

Popular posts from this blog

matlab - "Contour not rendered for non-finite ZData" -

delphi - Indy UDP Read Contents of Adata -

javascript - Any ideas when Firefox is likely to implement lengthAdjust and textLength? -