Skip to content

Instantly share code, notes, and snippets.

@bennylope
Created April 16, 2014 00:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bennylope/10791756 to your computer and use it in GitHub Desktop.
Save bennylope/10791756 to your computer and use it in GitHub Desktop.
Apply the `case_insenstive` Django view decorator if you have URLs which should match regardless of case, but you don't want those URLs hanging out there when a user visits.
import string
from django.http import HttpResponseRedirect, HttpResponsePermanentRedirect
def case_insensitive(func, case='lower', code=301):
"""
Django view function decorator which can enforce the case of a URL path by
redirecting to the properly cased URL. This *allows* for case insensitive
matches while ensuring that only a commonly cased-URL is used and seen.
"""
def inner(request, *args, **kwargs):
if case not in ['lower', 'upper']:
raise ValueError("{0} is not a valid case function: use 'lower' or 'upper'".format(case))
if code not in [301, 302]:
raise ValueError("{0} is not a valid HTTP redirect code".format(code))
redirect_klass = HttpResponseRedirect if code == 301 else HttpResponsePermanentRedirect
cased_path = getattr(string, case)(request.path)
if request.path != cased_path:
url = cased_path
if 'QUERY_STRING' in request.META:
url = "{0}?{1}".format(url, request.META['QUERY_STRING'])
return redirect_klass(url)
return func(request, *args, **kwargs)
return inner
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment