我如何要求此 Django 视图使用 HTTPS?

发布于 2024-10-14 06:34:22 字数 764 浏览 2 评论 0原文

(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
(r'^login/?

How do I add HTTPS required to this? I usually have a decorator for it..

But in this case I can't apply it.

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
,'django.contrib.auth.views.login',{'template_name':'login.html', 'authentication_form':CustomAuthenticationForm}),

How do I add HTTPS required to this? I usually have a decorator for it..

But in this case I can't apply it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

只是我以为 2024-10-21 06:34:22

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

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}),
)

I believe you can wrap the function in this manner:

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}),
)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文