Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I require HTTPS for this Django view?

(r'^login/?$','django.contrib.auth.views.login',{'template_name':'login.html', 'authentication_form':CustomAuthenticationForm}),

How do I add HTTPS required to this? I usually have a decorator for it..

But in this case I can't apply it.

def secure_required(view_func):
    """Decorator makes sure URL is accessed over https."""
    def _wrapped_view_func(request, *args, **kwargs):
        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 view_func(request, *args, **kwargs)
    return _wrapped_view_func
like image 435
TIMEX Avatar asked Jan 25 '11 05:01

TIMEX


People also ask

How can I test HTTPS connections with Django?

To try it out, just point your browser to http://localhost:8000 for normal HTTP traffic, and https://localhost:8443 for HTTPS traffic.

Does Django use HTTP or HTTPS?

The runserver command only handles http. However if you have SECURE_SSL_REDIRECT set to True then you will be redirected from http to https. See the Django docs on SSL/HTTPS for more information.

What is request in Django views?

Django uses request and response objects to pass state through the system. When a page is requested, Django creates an HttpRequest object that contains metadata about the request. Then Django loads the appropriate view, passing the HttpRequest as the first argument to the view function.


1 Answers

I believe you can wrap the function in this manner:

from django.contrib.auth.views import login
from <<wherever>> import secure_required


urlpatterns = patterns('',
    (r'^login/?$',secure_required(login),{'template_name':'login.html', 'authentication_form':CustomAuthenticationForm}),
)
like image 125
Jordan Reiter Avatar answered Sep 28 '22 07:09

Jordan Reiter