在Django中将http重定向到https(使用sslserver) [英] Redirect http to https in Django (using sslserver)

查看:227
本文介绍了在Django中将http重定向到https(使用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.

  1. 创建了一个新的Django项目
  2. 已将SECURE_SSL_REDIRECT = True添加到settings.py文件.现在,在这里,当我尝试使用http运行服务器时,它将重定向到https.但是到目前为止,我的服务器不支持https,导致未显示所需的网页.
  3. 已安装sslserver
  4. 使用命令python manage.py runsslserver 8000
  5. 运行项目
  6. 即使我使用http
  7. 打开网址,此操作也会成功将网页重定向到https
  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

此测试重定向可以像这样正常工作.但是,如果我已经安装了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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆