Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

looping in django using "in range(len()%4)

Tags:

python

django

I am trying to loop in Django templates using in range and modulo. This is my codes:

{% for iterate in range(len(items)%4) %}
    <div class="row">
        {% for item in items %}
            <div class="col-sm-3" style="background-color:lavender;">
              <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>
              <p> Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</p>
            </div>
        {% endfor %}
    </div>
{% endfor %}

But I got this error:

Could not parse the remainder: '(len(items)%4)' from 'range(len(items)%4)'

like image 893
Remarc Balisi Avatar asked Sep 13 '25 00:09

Remarc Balisi


1 Answers

Django doesn't allow complicated functions in it's templates, you need to create the range object in your python view, and pass to the template as a variable.

See the Django docs for explanation

Because Django intentionally limits the amount of logic processing available in the template language, it is not possible to pass arguments to method calls accessed from within templates. Data should be calculated in views, then passed to templates for display.

like image 110
Ritave Avatar answered Sep 15 '25 14:09

Ritave