Skip to content

Instantly share code, notes, and snippets.

@wachunei
Last active October 27, 2022 13:41
Show Gist options
  • Save wachunei/f7f1bb9bca0a74bb1053 to your computer and use it in GitHub Desktop.
Save wachunei/f7f1bb9bca0a74bb1053 to your computer and use it in GitHub Desktop.
Numeric for loop in Django Templates
<!-- Iterate N times (replace N with a number -->
{% for i in "x"|ljust:"N" %}
<!-- Access numeric variable (0-based index) -->
{{ forloop.counter0 }}
<!-- Access numeric variable (1-based index) -->
{{ forloop.counter }}
{% endfor %}
<!-- For example, iterate from 0 to 3 -->
{% for i in "x"|ljust:"3" %}
<!-- Do stuff -->
{% endfor %}
@wachunei
Copy link
Author

@bellydrum @kanik9 https://docs.djangoproject.com/en/3.1/ref/templates/builtins/#ljust

It is used only to make the string the size of the iteration.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment