如何为此Django视图要求HTTPS?

时间:2011-01-25 05:51:03

标签: python django http rest https

(r'^login/?$','django.contrib.auth.views.login',{'template_name':'login.html', 'authentication_form':CustomAuthenticationForm}),

如何添加此需要的HTTPS?我通常有一个装饰器..

但在这种情况下,我无法应用它。

def secure_required(view_func):
    """Decorator makes sure URL is accessed over https."""
    def _wrapped_view_func(request, *args, **kwargs):
        if not request.is_secure():
            if getattr(settings, 'HTTPS_SUPPORT', True):
                request_url = request.build_absolute_uri(request.get_full_path())
                secure_url = request_url.replace('http://', 'https://')
                return HttpResponseRedirect(secure_url)
        return view_func(request, *args, **kwargs)
    return _wrapped_view_func

1 个答案:

答案 0 :(得分:5)

我相信你可以用这种方式包装函数:

from django.contrib.auth.views import login
from <<wherever>> import secure_required


urlpatterns = patterns('',
    (r'^login/?$',secure_required(login),{'template_name':'login.html', 'authentication_form':CustomAuthenticationForm}),
)