I have user reviews on my site. Each review has a rating of 1-5 stars. I want to print that many stars. How do I do it? I only see {% for X in Y %}
which lets you iterate over a list, but not a certain number of times.
views:
89answers:
2
+1
A:
Use the Template range filter by zalun:
from django.template import Library
register = Library()
@register.filter
def get_range( value ):
"""
Filter - returns a list containing range made from given value
Usage (in template):
<ul>{% for i in 3|get_range %}
<li>{{ i }}. Do something</li>
{% endfor %}</ul>
Results with the HTML:
<ul>
<li>0. Do something</li>
<li>1. Do something</li>
<li>2. Do something</li>
</ul>
Instead of 3 one may use the variable set in the views
"""
return range( value )
jball
2010-06-03 20:49:37
Kinda dumb that I need a custom filter.... but it works. Thanks.
Mark
2010-06-03 21:23:36
Yeah, it is dumb, but you do what you have to do...
jball
2010-06-03 21:28:31
+3
A:
No need for a custom filter - make_list
will do the job:
{% for i in '123'|make_list %}
Daniel Roseman
2010-06-03 21:33:26
I'm pretty sure this *won't* work. That returns `[1,2,3]` *not* `[0,1,2,...,122]`.
Mark
2010-06-04 00:37:58
I am the OP, and I'm pretty sure that's not what I requested... how is this looping a "certain number of times" or "X times" given an (integer) rating between 1 and 5?
Mark
2010-06-04 07:54:51