Reputation: 549
I am using template tags to divide two numbers. The problem occurs when the denominator is zero. I am trying to use if with operators but this seems to not be working.
<td class="data">${% if nnl_asof_mtdgoal %}{{ nl_mtdgoal.get_mtd_newloansgoal|div:nnl_asof_mtdgoal.get_mtd_numnewloansgoals |floatformat:"2"| intcomma }}{% endif %}
This gives me this error
File "C:\pycharm-virtenv\DjangoPostgres\lib\site-packages\mathfilters\templatetags\mathfilters.py" in div
72. return nvalue / narg
Exception Type: ZeroDivisionError at /reports/goals/
Exception Value: float division by zero
As it should since its division by zero. When I try this:
<td class="data">${% if nnl_asof_mtdgoal !="0" %}{{ nl_mtdgoal.get_mtd_newloansgoal|div:nnl_asof_mtdgoal.get_mtd_numnewloansgoals |floatformat:"2"| intcomma }}{% endif %}
</td>
I get this error:
Exception Type: TemplateSyntaxError at /reports/goals/
Exception Value: Could not parse the remainder: '!="0"' from '!="0"'
I know this has to be an easy one but I cannot seem to figure out how to only draw the table if the division is True, i.e. not dividing by zero.
Upvotes: 0
Views: 857
Reputation: 308829
You need a space after !=
in your template tag, i.e.
{% if nnl_asof_mtdgoal != "0" %}
If nnl_asof_mtdgoal
was an integer and not a string, you would do:
{% if nnl_asof_mtdgoal != 0 %}
Since you use div:nnl_asof_mtdgoal.get_mtd_numnewloansgoals
in your template, it looks as if nnl_asof_mtdgoal
is not an integer, so you should change the check to:
{% if nnl_asof_mtdgoal.get_mtd_numnewloansgoals != 0 %}
Note that Django encourages you to keep logic out of the templates. It might be better to try to shift the calculation (and checking for zero) to the view.
Upvotes: 2