[Answer]-Django templates are not showing values of annotations

1👍

your view:

def items(request):
    rooms = RoomList.objects.filter(user=request.user).annotate(total_price=Sum('item__price')) # You can filter objects and next add annotates. `annotate(total_price=Sum('item__price')` will add `total_price` to objects.

    return render(request, 'items.html', {'items': Item.objects.filter(user=request.user), 'rooms': rooms) # Returns filtered objects with added annotates.

and template:

<table class="table table-hover">
    <thead>
        <tr>
            <th>Room name</th>
            <th>Costs</th>
        </tr>
    </thead>
    <tbody>
        {% for roomlist in rooms %}
        <tr>
            <td>{{ roomlist.room_name }}</td>
            <td>{{ roomlist.total_price }}</td> {# Your added annotates #}
        </tr>
        {% endfor %}
    </tbody>
</table>

Leave a comment