Skip to content

Instantly share code, notes, and snippets.

@mgd020
Created July 7, 2016 01:39
Show Gist options
  • Save mgd020/ce8dfc4134e34ac7b9dd9d7d01fdf870 to your computer and use it in GitHub Desktop.
Save mgd020/ce8dfc4134e34ac7b9dd9d7d01fdf870 to your computer and use it in GitHub Desktop.
allow url encoding inside django template
from django import template
from django.conf import settings
from django.utils.http import urlencode
register = template.Library()
@register.simple_tag(name='urlencode')
def urlencode_(*args, **kwargs):
if settings.DEBUG:
for arg in args:
if arg in kwargs:
raise ValueError("urlencode got multiple values for attribute '{}'".format(arg))
args = urlencode({arg:'' for arg in args}).replace('=', '')
kwargs = urlencode(kwargs)
if args and kwargs:
return '{}&{}'.format(args, kwargs)
return args or kwargs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment