Need to convert a string to int in a django template

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

    Yes, the place for this is in the view.

    I feel like the above example won't work -- you can't iterate over an integer.

    numItems = request.GET.get('numItems')
    
    if numItems:
       numItems = range(1, int(numItems)+1)
    
    return direct_to_template(request, "mytemplate.html", {'numItems': numItems})
    
    
    {% for item in numItems %}
     {{ item }}
    {% endfor %}
    

提交回复
热议问题