【问题标题】:Django redirecting http -> httpsDjango 重定向 http -> https
【发布时间】:2018-08-13 05:17:49
【问题描述】:

我正在跑步:

python manage.py runserver localhost:44100

这会将我重定向到https

» http http://localhost:44100/
HTTP/1.0 301 Moved Permanently
Content-Type: text/html; charset=utf-8
Date: Mon, 05 Mar 2018 14:09:09 GMT
Location: https://localhost:44100/
Server: WSGIServer/0.1 Python/2.7.14
X-Frame-Options: SAMEORIGIN

为什么/如何发生这种情况?什么设置控制Django 是否接受http / https

【问题讨论】:

    标签: python django http redirect https


    【解决方案1】:

    runserver 命令只处理 http。

    但是,如果您将 SECURE_SSL_REDIRECT 设置为 True,那么您将被从 http 重定向到 https。

    有关更多信息,请参阅SSL/HTTPS 上的 Django 文档。

    【讨论】:

    • 我如何将它恢复到以前的设置以在开发模式下进行测试?
    • @RemoBajwa 您可以设置SECURE_SSL_REDIRECT=False 或从您的设置中删除该行。另一种选择可能是将SECURE_SSL_REDIRECT = True 移动到像if not DEBUG: 这样的if 语句中。更改设置后,请清除浏览器缓存,使其不会继续重定向。
    【解决方案2】:

    我最好的猜测是你在项目的settings.py 文件中设置了

    SECURE_SSL_REDIRECT = True
    

    这会导致您的 http 重定向到 https。你可以阅读它here

    如果是这种情况,您可能需要删除该行并在它开始按预期工作之前清除浏览器缓存。

    【讨论】:

    • 这个和我一起工作,我使用 Heroku 作为 Django 应用程序的主机。
    • “清除浏览器缓存”救了我的命。谢谢@Saransh
    • @AshishJohnson:很高兴能提供帮助,因为如果没有清除缓存,我也会遇到类似的问题。
    【解决方案3】:

    使用python manage.py runssslserver 处理 SSL 和 https。 必须先安装django-sslserver 并设置进去

    INSTALLED_APPS = [
    'sslserver',
    ]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-12-20
      • 2012-01-10
      • 2011-09-06
      • 2018-08-29
      • 2016-10-30
      • 2011-05-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多