简体   繁体   中英

Force Django to use HTTPS URLs when reversing

I am running my Django application in Cloud9 for development purposes using the usual ./manage.py runserver. But to the outside world, the app is accessible via a https:// URL.

The problem is that when I use the URL reverse function, the URLs that come back start with http:// (at least some of the time). When I try redirecting to one of those URLs, I get an error like this one in the console:

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.

My question: is there a way to force reverse to generate HTTPS URLs instead of HTTP?

Here is a snippet of code, which has problems with redirection from HTTPS URLs to HTTP ones:

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')

你应该看看django-sslify ,你只需安装它并将它添加到你的MIDDLEWARE_CLASSES

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM