'Redirect http to https in Django (using sslserver)

I have a django project working with HTTPS using django sslserver.I want http to be redirected to https. I tried adding SECURE_SSL_REDIRECT = True which does not seem to have any effect.

Similarly to test if my redirection was right, I tried the following on a test project.

  1. Created a new django project
  2. Added SECURE_SSL_REDIRECT = True to the settings.py file. Here now, when I try to run the server with http it redirects to https. But asof now my server does not support https cause of which desired web page is not displayed.
  3. Installed sslserver
  4. Ran the project with the command python manage.py runsslserver 8000
  5. This succesfully redirects the webpage to https even if I open the url with http

This test redirection works fine like this. But if I already have sslserver installed ssl redirection does not seem to have any effect. I have been stuck with this problem for a while now and would really appreciate some help.



Solution 1:[1]

SECURE_SSL_REDIRECT settings works together with SecurityMiddleware.

Try add it to MIDDLEWARE_CLASSES in settings.py.

Solution 2:[2]

In addition to SECURE_SSL_REDIRECT=True in settings.py as mentioned, you need both

  1. runsslserver on port 443
  2. runserver on port 80

To have another service listen on port 80 for incoming http requests and do the redirect.

That works for me.

Solution 3:[3]

try adding to your settings.

ACCOUNT_DEFAULT_HTTP_PROTOCOL = 'https'

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Vladimir Osintsev
Solution 2 jsu
Solution 3 Mint