Skip to content

Instantly share code, notes, and snippets.

@chrisjones-brack3t
Last active August 29, 2015 14:19
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 chrisjones-brack3t/08a01b1f85e841ab3ef0 to your computer and use it in GitHub Desktop.
Save chrisjones-brack3t/08a01b1f85e841ab3ef0 to your computer and use it in GitHub Desktop.
SSLRedirectMixin
class SSLRequiredMixin(object):
""" """
raise_exception = False # Default whether to raise an exception to none
def dispatch(self, request, *args, **kwargs):
# if getattr(settings, 'DEBUG', False):
# return super(SSLRequiredMixin, self).dispatch(
# request, *args, **kwargs)
if not request.is_secure():
if self.raise_exception:
raise Http404
return HttpResponsePermanentRedirect(
self._build_https_url(request))
return super(SSLRequiredMixin, self).dispatch(request, *args, **kwargs)
def _build_https_url(self, request):
""" Get the full url, replace http with https """
url = request.build_absolute_uri(request.get_full_path())
return re.sub(r'^http', 'https', url)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment