【问题标题】:Using https as standard with django project在 django 项目中使用 https 作为标准
【发布时间】:2017-11-28 07:19:12
【问题描述】:

我正在学习 django 并尝试完成我的第一个 web 应用程序。

我正在使用 shopify api 和锅炉板 (starter code),但在身份验证的最后一步遇到问题。

具体来说,重定向 URL —— 它在不应该使用 HTTP:// 时使用了 HTTP://,我不知道如何更改它..

#in my view

def authenticate(request):

    shop = request.GET.get('shop')

    print('shop:', shop)
    if shop:
        scope = settings.SHOPIFY_API_SCOPE
        redirect_uri = request.build_absolute_uri(reverse('shopify_app_finalize')) #try this with new store url?
        print('redirect url', redirect_uri) # this equals http://myherokuapp.com/login/finalize/
        permission_url = shopify.Session(shop.strip()).create_permission_url(scope, redirect_uri)
        return redirect(permission_url)

    return redirect(_return_address(request))

这是一个问题,因为我的应用使用嵌入式 Shopify SDK,这会导致在此请求时发生此错误 Refused to frame 'http://my.herokuapp.com/' because it violates the following Content Security Policy directive: "child-src 'self' https://* shopify-pos://*". Note that 'frame-src' was not explicitly set, so 'child-src' is used as a fallback.

如何更改 URL 以使用 HTTPS?

提前非常感谢您。如果我可以分享任何其他细节,请告诉我,但我的代码实际上与 starter code 相同。

【问题讨论】:

    标签: python django shopify


    【解决方案1】:

    这就是 Django 文档对build_absolute_uri 所说的话:

    不鼓励在同一站点上混合使用 HTTP 和 HTTPS,因此 build_absolute_uri() 将始终生成一个绝对 URI,其中 当前请求具有相同的方案。如果您需要将用户重定向到 HTTPS,最好让您的 Web 服务器将所有 HTTP 流量重定向到 HTTPS。

    所以你可以做两件事:

    1. 确保您的站点完全在 HTTPS 上运行(首选选项):设置您的 Web 服务器以使用 HTTPS,请参阅 Heroku 文档以了解如何执行此操作。如果传入的请求是在 HTTPS 上,Django 将自动为 request.build_absolute_uri 使用 HTTPS。 我不确定shop 参数中传递了什么,但如果它包含个人数据,我还是建议使用 HTTPS。

    2. 自己创建网址:

      url = "https://{host}{path}".format(
          host = request.get_host(),
          path = reverse('shopify_app_finalize'))
      

      但您仍需要将服务器配置为接受传入的 HTTPS 请求。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-07-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-27
      • 1970-01-01
      • 2018-03-29
      • 1970-01-01
      相关资源
      最近更新 更多