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)'
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With