强制Django在撤消时使用HTTPS URL

时间:2016-02-04 12:29:28

标签: python django python-2.7 cloud9-ide django-1.6

我正在使用通常的./manage.py运行服务器在Cloud9中运行我的Django应用程序以进行开发。但对于外部世界,可以通过https:// URL访问该应用程序。

问题在于,当我使用URL反向功能时,返回的URL以http://开头(至少在某些时候)。当我尝试重定向到其中一个URL时,我在控制台中收到类似这样的错误:

Mixed Content: The page at 'https://apps.facebook.com/xxxx/' 
was loaded over HTTPS, but requested an insecure form action 
'http://xxxx.c9users.io/facebook_app/gift_shop/'. 
This request has been blocked; the content must be served over HTTPS.

我的问题:有没有办法强制反向生成HTTPS网址而不是HTTP?

以下是代码片段,其中包含从HTTPS网址重定向到HTTP网址的问题:

class IndexRedirectView(RedirectView, CSRFExemptMixin):
    permanent = False

    def get_redirect_url(self, *args, **kwargs):
        if self.request.user.visit_count >= 5:
            return reverse('gift-shop')
        if len(BaseGiftableInstance.objects.filter(giving_user=self.request.user)) > 0:
            # has won something
            return reverse('gift-shop')
        return reverse('spinner')

2 个答案:

答案 0 :(得分:6)

您应该结帐django-sslify,然后安装它并将其添加到Model Class

答案 1 :(得分:5)

如果您使用的是Django 1.8或更高版本,则可以使用SECURE_SSL_REDIRECT = True设置强制使用SSL - 请参阅类似问题的this answer