Need to convert a string to int in a django template

you can coerce a str to an int using the add filter

{% for item in numItems|add:"0" %}

https://docs.djangoproject.com/en/dev/ref/templates/builtins/#add

to coerce int to str just use slugify

{{ some_int|slugify }}

EDIT: that said, I agree with the others that normally you should do this in the view – use these tricks only when the alternatives are much worse.

Leave a Comment