在Django中将http重定向到https(使用sslserver) [英] Redirect http to https in Django (using sslserver)
问题描述
我有一个使用django使用HTTPS的django项目 sslserver .我想要http
重定向到https
.我尝试添加似乎没有任何作用的SECURE_SSL_REDIRECT = True
.
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.
- 创建了一个新的Django项目
- 已将
SECURE_SSL_REDIRECT = True
添加到settings.py
文件.现在,在这里,当我尝试使用http
运行服务器时,它将重定向到https
.但是到目前为止,我的服务器不支持https
,导致未显示所需的网页. - 已安装
sslserver
- 使用命令
python manage.py runsslserver 8000
运行项目
- 即使我使用
http
打开网址,此操作也会成功将网页重定向到
https
- Created a new django project
- Added
SECURE_SSL_REDIRECT = True
to thesettings.py
file. Here now, when I try to run the server withhttp
it redirects tohttps
. But asof now my server does not supporthttps
cause of which desired web page is not displayed. - Installed
sslserver
- Ran the project with the command
python manage.py runsslserver 8000
- This succesfully redirects the webpage to
https
even if I open the url withhttp
此测试重定向可以像这样正常工作.但是,如果我已经安装了sslserver
,则ssl重定向似乎没有任何效果.我已经被这个问题困扰了一段时间了,非常感谢您的帮助.
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.
推荐答案
SECURE_SSL_REDIRECT
设置与SecurityMiddleware
一起使用.
尝试将其添加到settings.py
中的MIDDLEWARE_CLASSES
.
这篇关于在Django中将http重定向到https(使用sslserver)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!