Need to convert a string to int in a django template

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

    I like making a custom filter:

    # templatetags/tag_library.py
    
    from django import template
    
    register = template.Library()
    
    @register.filter()
    def to_int(value):
        return int(value)
    

    Usage:

    {% load tag_library %}
    {{ value|to_int }}
    

    It is for cases where this cannot be easily done in view.

提交回复
热议问题