Skip to content Skip to sidebar Skip to footer

Django Print Loop Value Only Once

I have a view where I am getting the list of appointments with limit.. def HospitalAppointmentView(request, pk, username, hdpk): todays_appointments = DoctorAppointment.objects.fil

Solution 1:

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%}

Solution 2:

{{todays_appointments.0.doctor.username}}

and your normal loop.

{% for appointment in todays_appointments %}
 ....
{% endfor %}

but rohan's answer is wiser

Post a Comment for "Django Print Loop Value Only Once"