Modulo In Django Templates
Ever find yourself wishing for the modulo operator (%) in Django templates? I did. Its easy enough to create your own custom filter so why not do it?
Caveat: I couldn't figure out how to get the filter to accept two arguments to do the modulo test against so I defaulted the test value to zero. If there is a way to add a second argument I would love to add that to the code.
In the templatetags directory of your Django install, add a file named 'mod.py'. In that file add the following code:
from django import template
register = template.Library()
def mod(value, arg):
if value % arg == 0:
return True
else:
return False
register.filter('mod', mod)
In your template use the mod filter like this:
...
{% load mod %}
...
<tr bgcolor="{% if forloop.counter|mod:2 %}#cccccc{% else %}#ffffff">
...
This will alternate every other row's background color between gray and white.
![[link:home]](http://thebuckpasser.com/images/home.png)
![[link:about me]](http://thebuckpasser.com/images/aboutme.png)
![[link:rankings]](http://thebuckpasser.com/images/rankings.png)



<tr bgcolor="{% cycle #ccc,#fff %}">
or
<tr class="{% cycle odd,even %}">
{% if forloop.counter|divisibleby:"2" %}
Which is equivalent to your '|mod:2'.
http://www.uggboots-space.com uggs outlet
http://www.uggboots-space.com uggs outlet stores
http://www.uggboots-space.com ugg outlet store
http://www.uggboots-space.com cheap ugg boots
http://www.uggboots-onsale.net ugg on sale
http://www.uggs-online.net uggs on sale
http://www.ugg-sales.net uggs on sale
http://www.nikestarsonsale.com basketball shoes
http://www.uggbootshome.net ugg boots
http://www.uggboots-online.net ugg boots online
http://www.4unj.com/ nfl jerseys
http://www.uggbootshome.net Uggs Outlet
http://www.uggbootshome.net Ugg Outlet
http://www.uggbootshome.net Ugg Outlet Store
http://www.uggbootshome.net Uggs Outlet Stores
http://www.uggbootshome.net Ugg Boots Outlet
http://www.uggbootshome.net Outlet Ugg Boots
http://www.uggbootshome.net Wholesale Ugg Boots
http://www.saleairmax.com air max
http://www.saleairmax.com nike max
http://www.saleairmax.com max shoes