简体   繁体   中英

Making an HTTPS site using Django

I'm using Django to build a site and I want one part of my site to be in HTTPS.

In my settings.py file, I have

SESSION_COOKIE_SECURE = True

CSRF_COOKIE_SECURE = True

SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')

In my wsgi.py file, I have os.environ['HTTPS'] = "on"

Then in my views.py for the part of the site I'm working on, I'm using a decorator to force everything to be redirected to https.

def secure_required(view_func):
    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

However, when I try to load the page on my localhost, the page doesn't load and I just get an error that says "This webpage is not available." What am I missing to enable HTTPS for my website?

The webserver log shows this:

13:38:22 web.1     | 2013-08-02 13:38:22 [48421] [CRITICAL] WORKER TIMEOUT (pid:48423) 
13:38:22 web.1     | 2013-08-02 13:38:22 [48421] [CRITICAL] WORKER TIMEOUT (pid:48423)
13:38:22 web.1     | 2013-08-02 13:38:22 [48444] [INFO] Booting worker with pid: 48444

原来https在我以前不知道的localhost上不起作用。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM