[Answer]-Accessing list item in django template

1👍

The best option is to use choices attribute for the rating field:

RATING_CHOICES = list(enumerate(['', 'Disappointed', 'Not Promissing',
                                 'OK', 'Good', 'Awesome']))

class Review(models.Model):
    ...
    rating = models.IntegerField(..., choices=RATING_CHOICES)

And then use it in the template:

{{ review.get_index_display }}

The other option is to use custom template filter:

@register.filter
def get_by_index(lst, idx):
    return lst[idx]

Template will look like this:

{{ rate_text|get_by_index:review.rating }}

Leave a comment