Need to convert a string to int in a django template

前端 未结 6 1012
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-27 04:58


I am trying to pass in url parameters to a django template like this...

response = render_to_string(\'persistConTemplate.html\', request.GET)
         


        
6条回答
  •  难免孤独
    2020-11-27 05:40

    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.

提交回复
热议问题