Could not parse some characters: |(total/4)||floatformat:2

<td>
{% with data.marks1|add:data.marks2|add:data.marks3|add:data.marks4 as total %}
{{ (total/4)|floatformat:2 }}
{% endwith %}
I am making a simple crud application in Django and em repeatedly getting this error. Anything I do does not resolve this error

The error occurs because of the use of the pipe symbol | within the with tag, which Django's template engine cannot parse correctly in that context. Here's how you can fix the issue:

#create a custom filter template:
In your templatetags directory (if you don’t have one, create it), add a new file, e.g., math_filters.py:
 
from django import template
register = template.Library()
@register.filter
def div(value, arg):
    try:
        return float(value) / float(arg)
    except (ValueError, ZeroDivisionError):
        return None

# Now load the custom filter in your tempalte
{% load math_filters %}
<td>
{% with total=data.marks1|add:data.marks2|add:data.marks3|add:data.marks4 %}
    {{ total|div:"4"|floatformat:2 }}
{% endwith %}
</td>
Back to Top