Advertisement
Guest User

django for loop starts snippet

a guest
Mar 19th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.63 KB | None | 0 0
  1. # templatetags/utils.py
  2.  
  3. register = Library()
  4. @register.filter
  5. def stars(value):
  6.     max_val = 5
  7.     int_val = int(value)
  8.     difference = value - int_val
  9.  
  10.     output = ''
  11.     for c in range(int_val):
  12.         output+='f'
  13.  
  14.     if difference:
  15.         output+='h'
  16.  
  17.     for d in range(max_val-len(output)):
  18.         output+='e'
  19.  
  20.     return output
  21.  
  22.  
  23. # template
  24. {% load utils %}
  25.  
  26. {% for star in rank|stars %}
  27.     {% if star  == 'f' %}
  28.             <i class="fa fa-star" aria-hidden="true"></i>
  29.     {% elif star == 'h' %}
  30.         <i class="fa fa-star-half-o" aria-hidden="true"></i>
  31.     {% elif star == 'e' %}
  32.         <i class="fa fa-star-o" aria-hidden="true"></i>
  33.     {% endif %}
  34. {% endfor %}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement