0

I have the following section in my template where I make use of indexing my query set to access its fields. I know this can be done without indexing but I am trying whether this can be done by indexing. confirmed_length is a list which is [0] and confirmed_bookings is the query_set

    {% for i in confirmed_length %}
        <tr>
            <th scope="row">{{ confirmed_bookings.0.booking_id }}</th>
            <td>{{ i }}</td>
            <td>{{ confirmed_bookings.0.booking_date }}</td>
            <td>{{ confirmed_bookings.i.room_id }}</td>
        </tr>
    {% endfor %}

where {{ i }} shows 0 but nothing shows for others except
{{ confirmed_bookings.0.booking_date }} and {{ confirmed_bookings.0.booking_id }}. Indexing with 0 shows me the details but indexing with i (where i is an integer and its value is 0) does not show anything.

xyres
  • 20,487
  • 3
  • 56
  • 85
  • That will not work. You have at least 2 options: 1. Use the [Jinja2](http://jinja.pocoo.org/docs/2.9/) template engine or 2. Implement your own [template filter](https://stackoverflow.com/questions/4651172/reference-list-item-by-index-within-django-template). – nik_m Aug 27 '17 at 08:24
  • 2
    Possible duplicate of [Get list item dynamically in django templates](https://stackoverflow.com/questions/8948430/get-list-item-dynamically-in-django-templates) – Brown Bear Aug 27 '17 at 08:30
  • That won't work because when you write `{{ confirmed_bookings.i }}`, Django doesn't take the value of `i` from the loop. Instead it checks if `confirmed_bookings` has an attribute named `i`. Or if `confirmed_bookings` is a dict, does it have a key named `i`. When both the checks fail, Django renders nothing. *Source: [Docs](https://docs.djangoproject.com/en/1.10/ref/templates/api/#variables-and-lookups)* – xyres Aug 27 '17 at 08:53

0 Answers0