I have rendering one string in django template but in django template, I want to convert it into integer. I have tried many thing like |add:to_int but it doesn't work.
Asked
Active
Viewed 2,563 times
1
-
check this: https://stackoverflow.com/a/15820445 – Nalin Dobhal Dec 24 '19 at 07:00
-
Does this answer your question? [Need to convert a string to int in a django template](https://stackoverflow.com/questions/4831306/need-to-convert-a-string-to-int-in-a-django-template) – Abdul Aziz Barkat Nov 07 '22 at 05:09
2 Answers
3
@Vaibhav Mishra,
Does it work for you? https://docs.djangoproject.com/en/3.0/ref/templates/builtins/#add
Like: {% value|add:"0" %}
If it does not work for you: then create a custom template filter and register it:
YOUR_APP_FOLDER/
__init__.py
models.py
templatetags/
__init__.py
your_app_name_extras.py [any valid filename.py]
views.py
in your_app_name_extras.py
from django import template
register = template.Library()
@register.filter()
def to_int(value):
return int(value)
In your template file, you need to put the code:
{% load your_app_name_extras %}
{{ value|to_int }}
For more details:
https://docs.djangoproject.com/en/3.0/howto/custom-template-tags/

Iqbal Hussain
- 1,077
- 1
- 4
- 12
0
You can do like that:
{% if string_value == int_value|stringformat:'s' %} {% endif %}

Shaiful Islam
- 335
- 2
- 12