Adding percentage of student marks in display table in a crud program in Django

<table>
    {% for data in result %}
    <tr>
        <td>{{ data.id }}</td>
        <td>{{ data.name }}</td>
        <td>{{ data.marks1 }}</td>
        <td>{{ data.marks2 }}</td>
        <td>{{ data.marks3 }}</td>
        <td>{{ data.marks4 }}</td>
        <td>
            {% with data.marks1|add:data.marks2|add:data.marks3|add:data.marks4 as total %}
                {{ total }}
            {% endwith %}
        </td>
    </tr>
    {% endfor %}
</table>

//show.html(above code) - This is without the math-filters function

//views.py

from django.http import HttpResponse
from django import template
from django.shortcuts import render
from .models import Student


def add(request):
    if request.method == 'POST':
        if request.POST.get('name') and request.POST.get('marks1'):
            stud = Student()
            stud.name = request.POST.get('name')
            stud.marks1 = request.POST.get('marks1')
            stud.marks2 = request.POST.get('marks2')
            stud.marks3 = request.POST.get('marks3')
            stud.marks4 = request.POST.get('marks4')
            stud.save()
            return render(request, 'add.html')
    else:
        return render(request, 'add.html')


def show(request):
    data = Student.objects.all()
    return render(request, 'show.html', {'result': data})


register = template.Library()


@register.filter
def div(value, arg):
    try:
        return float(value) / float(arg)
    except (ValueError, ZeroDivisionError):
        return None

The add.html file works perfectly fine. I tried adding this after the td tag which has the total function hoping it would work but it ends up displaying an error.

{% load math_filters %}
        <td>
        {% with total=data.marks1|add:data.marks2|add:data.marks3|add:data.marks4 %}
            {{ total|div:"4"|floatformat:2 }}
        {% endwith %}
        </td>

enter image description here
Image of the error that occurs.

Back to Top