Skip to content

Instantly share code, notes, and snippets.

@rozza
Created August 19, 2010 09:07
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 rozza/537450 to your computer and use it in GitHub Desktop.
Save rozza/537450 to your computer and use it in GitHub Desktop.
from django.conf import settings
from django.http import HttpResponseRedirect
def secure_required(func):
"""
Decorator makes sure URL is accessed over https.
Use with `SecureRequiredMiddleware` to ensure only decorated urls are
accessed via https
"""
def wrap(request, *args, **kwargs):
request.secure_required = True
if not request.is_secure():
if getattr(settings, 'HTTPS_SUPPORT', True):
request_url = request.build_absolute_uri(request.get_full_path())
secure_url = request_url.replace('http://', 'https://')
return HttpResponseRedirect(secure_url)
return func(request, *args, **kwargs)
return wrap
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment