Skip to content

Instantly share code, notes, and snippets.

@bulv1ne
Created May 20, 2017 11:32
Show Gist options
  • Save bulv1ne/3304057a22aab591532e619a7dc50d0f to your computer and use it in GitHub Desktop.
Save bulv1ne/3304057a22aab591532e619a7dc50d0f to your computer and use it in GitHub Desktop.
Django random templatetag for random integers (mainly used in cache busting)
import random
from django import template
register = template.Library()
@register.simple_tag
def random_int(a, b=None):
'''
Return a random integer N such that `a <= N <= b`
If b is omitted, N is `a / 2 <= N <= a`
'''
if not b:
a, b = a // 2, a
return random.randint(a, b)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment