Skip to content Skip to sidebar Skip to footer

Display Sum Per User Elegantly In Django

trying to display the final mark per student. But instead of just showing the result of sum , it displays a chunk of query set: for example: Annie

Solution 1:

The below should return a queryset with student names and marks. Two separate queries should not be needed.

students = MarkAtt.objects.values('studName').annotate(mark=Sum('attendance'))

If studName is a foreign key, do this .values('studName__name')

Then in your context you can just add:

context = {
    students = students
}

You can then loop through students in you template and display data as:

<p>{{x.studName}}</p> <p> {{x.mark}}</p>

Solution 2:

You are overcomplicating things. Given the following simple context:

students = MarkAtt.objects.values('studName').annotate(mark=Sum('attendance'))
context = {
    'students' : students,
}

you can do in the template:

{% for student in students %}  
    <p>{{student.studName}}</p> <p> {{ student.mark}}</p>
{% endfor %}

Post a Comment for "Display Sum Per User Elegantly In Django"