[英]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. 我正在使用通常的./manage.py运行服务器在Cloud9中运行我的Django应用程序以进行开发。 But to the outside world, the app is accessible via a https:// URL.
但对于外部世界,可通过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). 问题是,当我使用URL反向函数时,返回的URL以http://开头(至少在某些时候)。 When I try redirecting to one of those URLs, I get an error like this one in the console:
当我尝试重定向到其中一个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.
My question: is there a way to force reverse to generate HTTPS URLs instead of HTTP? 我的问题:有没有办法强制反向生成HTTPS URL而不是HTTP?
Here is a snippet of code, which has problems with redirection from HTTPS URLs to HTTP ones: 这是一段代码,从HTTPS URL到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')
你应该看看django-sslify ,你只需安装它并将它添加到你的MIDDLEWARE_CLASSES
。
如果您使用的是Django 1.8或更高版本,则可以使用设置SECURE_SSL_REDIRECT = True
强制使用SSL - 请参阅类似问题的答案
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.