我已经在Linode上使用了以下SSLMiddleware一段时间了,我的SSL工作得很好,现在我已经将我的服务器更改为Webfaction了,突然间,我的HTTPS页面无法正常工作它正确地重定向到https页面,但我所有的css文件,css文件中的图像(没有绝对的url),javascript都变成了非安全源(指的是http://而不是https://),我真的我现在感到困惑,因为我不知道它是否与SSLMiddleware或其他东西有关,我在settings.py中没有改变任何东西,除了数据库参数值..请帮忙。提前谢谢。
__license__ = "Python"
__copyright__ = "Copyright (C) 2007, Stephen Zabel"
__author__ = "Stephen Zabel - sjzabel@gmail.com"
__contributors__ = "Jay Parlar - parlar@gmail.com"
from django.conf import settings
from django.http import HttpResponseRedirect, HttpResponsePermanentRedirect, get_host
SSL = 'SSL'
class SSLRedirect:
def process_view(self, request, view_func, view_args, view_kwargs):
if SSL in view_kwargs:
secure = view_kwargs[SSL]
del view_kwargs[SSL]
else:
secure = False
if settings.ENABLE_SSL:
if not secure == self._is_secure(request):
return self._redirect(request, secure)
else:
return
def _is_secure(self, request):
if request.is_secure():
return True
#Handle the Webfaction case until this gets resolved in the request.is_secure()
if 'HTTP_X_FORWARDED_SSL' in request.META:
return request.META['HTTP_X_FORWARDED_SSL'] == 'on'
return False
def _redirect(self, request, secure):
protocol = secure and "https" or "http"
newurl = "%s://%s%s" % (protocol,get_host(request),request.get_full_path())
if settings.DEBUG and request.method == 'POST':
raise RuntimeError, \
"""Django can't perform a SSL redirect while maintaining POST data.
Please structure your views so that redirects only occur during GETs."""
return HttpResponsePermanentRedirect(newurl)
答案 0 :(得分:0)
我最近在WebFaction上实现了SSL而没有任何自定义中间件修补,这是一个非常简单的过程。
看看这里:http://community.webfaction.com/questions/512/how-do-i-set-up-a-https-ssl-django-site
如果这没有帮助,请与他们一起打开一张票。他们通常非常善于很快解决问题。