0

In template, I try to use:

{% for i in range(object.punctuation) %}
    <i class="material-icons">star</i> 
{% endfor %}

Where object.punctuation is an integer with the rating that the object has. Visually this punctuation needs to be represented with stars. But I get:

Could not parse the remainder: '(object.punctuation)' from 'range(object.punctuation)'

As cited in: https://jbmoelker.github.io/jinja-compat-tests/functions/range/#stop looks like range function in jinja has problems with django integration. Is there an alternative to accomplish what I'm trying to do?

Johan
  • 3,577
  • 1
  • 14
  • 28
Rizel Tane
  • 99
  • 3
  • 12
  • This has nothing to do with Jinja because *you are not using Jinja*. You are using Django template language, which as all the documentation including the tutorial explains, does not allow you to call functions with parameters. – Daniel Roseman Oct 01 '18 at 19:05

2 Answers2

0

In your view you can have a variable

my_range = range(object.punctuation)
context['my_range'] = my_range

and then pass it to template from context. Then loop over as follows:

{% for i in my_range %}
    <i class="material-icons">star</i> 
{% endfor %}
Ojas Kale
  • 2,067
  • 2
  • 24
  • 39
  • Got amazed cause it worked for a single object. But now I'm dealing with modifying each object item data, since it is the result of looping from a query list – Rizel Tane Oct 01 '18 at 19:10
0

Daniel's correction helped. Found my solution here: Numeric for loop in Django templates

{% with ''|center:object.punctuation as range %}
    {% for _ in range %}
    <i class="material-icons">star</i>
    {% endfor %}
{% endwith %}
Rizel Tane
  • 99
  • 3
  • 12