Skip to content

Instantly share code, notes, and snippets.

@philippeowagner
Forked from ckinsey/decorators.py
Last active August 29, 2015 14:08
Show Gist options
  • Select an option

  • Save philippeowagner/9de77c7e3f2d16970a55 to your computer and use it in GitHub Desktop.

Select an option

Save philippeowagner/9de77c7e3f2d16970a55 to your computer and use it in GitHub Desktop.
from functools import wraps
from django.conf import settings
from django.http import HttpResponseRedirect
def require_ssl(view):
"""
Decorator that requires an SSL connection. If the current connection is not SSL, we redirect to the SSL version of
the page.
"""
@wraps(view)
def wrapper(request, *args, **kwargs):
if not settings.DEBUG and not request.is_secure():
# Redirect!
target_url = "https://" + request.META['HTTP_HOST'] + request.path_info
return HttpResponseRedirect(target_url)
return view(request, *args, **kwargs)
return wrapper
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment