When parsing arguments to filters, Django converts numeric literals to float
or int
, but in the case of scientific notation for float
this is incomplete:
>>> from django.template.base import Template
>>> from django.template.engine import Engine
>>> 5.2e3
5200.0
>>> Template("{{ foo|default:5.2e3 }}", engine=e)
<Template template_string="{{ foo|default:5.2e3...">
>>> 5.2e-3
0.0052
>>> Template("{{ foo|default:5.2e-3 }}", engine=e)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/lily/.local/share/lilyenv/virtualenvs/django-rusty-templates/3.12/lib/python3.12/site-packages/django/template/base.py", line 154, in __init__
self.nodelist = self.compile_nodelist()
^^^^^^^^^^^^^^^^^^^^^^^
File "/home/lily/.local/share/lilyenv/virtualenvs/django-rusty-templates/3.12/lib/python3.12/site-packages/django/template/base.py", line 196, in compile_nodelist
nodelist = parser.parse()
^^^^^^^^^^^^^^
File "/home/lily/.local/share/lilyenv/virtualenvs/django-rusty-templates/3.12/lib/python3.12/site-packages/django/template/base.py", line 489, in parse
raise self.error(token, e)
File "/home/lily/.local/share/lilyenv/virtualenvs/django-rusty-templates/3.12/lib/python3.12/site-packages/django/template/base.py", line 487, in parse
filter_expression = self.compile_filter(token.contents)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/lily/.local/share/lilyenv/virtualenvs/django-rusty-templates/3.12/lib/python3.12/site-packages/django/template/base.py", line 605, in compile_filter
return FilterExpression(token, self)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/lily/.local/share/lilyenv/virtualenvs/django-rusty-templates/3.12/lib/python3.12/site-packages/django/template/base.py", line 706, in __init__
raise TemplateSyntaxError(
django.template.exceptions.TemplateSyntaxError: Could not parse the remainder: '-3' from 'foo|default:5.2e-3'
I couldn't actually find the documentation for Django's handling of numeric literals in templates, so this may be an undocumented implementation detail. If so, perhaps deprecating scientific notation would be appropriate. Otherwise, we should support this case.
I am beginner in os , Giving this a try