Need to convert a string to int in a django template

前端 未结 6 1011
爱一瞬间的悲伤
爱一瞬间的悲伤 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:55

    My solution is kind of a hack and very specific..

    In the template I want to compare a percentage with 0.9, and it never reaches 1, but all the values are considered string in the template, and no way to convert string to float.

    So I did this:

    {% if "0.9" in value %}
    ...
    {% else %}
    ...
    {% endif %}
    

    If I want to detect some value is beyond 0.8, I must do:

    {% if ("0.9" in value) or ("0.8" in value) %}
    ...
    {% else %}
    ...
    {% endif %}
    

    This is a hack, but suffice in my case. I hope it could help others.

提交回复
热议问题