How to force the use of SSL for some URL of my Django Application?

前端 未结 4 1017
北海茫月
北海茫月 2020-12-08 12:20

I want to be sure that for some URL of my website, SSL will be use. I saw a lot of answer already on SO.

Force redirect to SSL for all pages apart from one

S

4条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-08 12:45

    Here's a view decorator that you can apply to the views that should have HTTPS.

    from functools import wraps
    from django.conf import settings
    from django.http import HttpResponseRedirect
    
    
    def require_https(view):
        """A view decorator that redirects to HTTPS if this view is requested
        over HTTP. Allows HTTP when DEBUG is on and during unit tests.
    
        """
    
        @wraps(view)
        def view_or_redirect(request, *args, **kwargs):
            if not request.is_secure():
                # Just load the view on a devserver or in the testing environment.
                if settings.DEBUG or request.META['SERVER_NAME'] == "testserver":
                    return view(request, *args, **kwargs)
    
                else:
                    # Redirect to HTTPS.
                    request_url = request.build_absolute_uri(request.get_full_path())
                    secure_url = request_url.replace('http://', 'https://')
                    return HttpResponseRedirect(secure_url)
    
            else:
                # It's HTTPS, so load the view.
                return view(request, *args, **kwargs)
    
        return view_or_redirect
    

提交回复
热议问题