Extending the for loop in Django templates
Posted by Martin Brochhaus | Filed under Tutorials
Django comes with an excellent templating language which has many built-in template tags and filters. Although it is very powerful already there are a few scenarios which cannot be solved, yet. As far as I know the for-loop can only iterate over a list of objects. You are not able to use a C-like for-loop like so:
for(int i=0; i<20; i++)
Luckily Django allows us to extend the templating language with custom filters. I will show you how you can create a classic for-loop for Django templates by implementing three simple steps…
Step 1: Create a custom filters application
To do this just type “python manage.py startapp custom_filters”. After that add the new application to your settings.py:
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.admin',
'sample_project.home',
'sample_project.custom_filters',
)
You should have a new directory called “custom_filters” in your project folder. Navigate into this directory and add another directory called “templatetags”. Within this directory add two files: “__init__.py” and “custom_filters.py”. You should have the following directory structure now:

Step 2: Implement the custom filter
Simply add the follwoing code to your “custom_filters.py”. If you want to learn more about writing custom filters you should have a closer look at the enlightening official guide to custom template tags and filters.
from django import template
register = template.Library()
@register.filter
def django_range(value):
return range(value)
Step 3: Use your magic new for-loop
Now you are ready to enjoy your work. Use the following code in your template file.
{% load custom_filters %}
{% for i in 20|django_range %}
{{ i }}
{% endfor %}
What does it do? It’s simple: First you load your custom_filters module (note: in this example both the application and the module are named custom_filters – sorry for that!). After that you can use your new filter just like the built-in ones. “20″ is the variable which gets passed into the filter function as “value”.
I came up with this solution when developing the image galleries at http://www.blumenristau.de. As you can see there are always twenty ’slots’ for thumbnails no matter how many images have been uploaded. By using the built-in filter “length” it was easy to determine the number of currently shown images. The next thing needed to draw the empty slots was iterating “for i=X to 20″- That was when I started wondering…
Tags: django, filter, for-loop, template, tutorial








April 20th, 2009 at 7:51 am
Thanks for this tutorial. We do need something like this. We’ll try it out.
Raghu
May 8th, 2009 at 9:33 pm
Exactly how I was thinking to code it. Thanks for the tutorial and saving me some extra keystrokes. :)
September 8th, 2009 at 10:47 pm
Incredible how ugly a language can be… I hope one day you’ll wake up and realized the truth you used to know–nothing beats c#
April 7th, 2010 at 7:13 pm
Hey, how did you do such a nice design and using django? Dreamweaver? thanks for the answer, just being curious.
April 21st, 2010 at 9:58 pm
@Rosalie:
Thank you for the complilment! I create my websites with Photoshop and then code HTML/CSS/JavaScript in a texteditor like notepad+