对阿贾克斯的看法login_required装饰返回401,而不是302 [英] login_required decorator on ajax views to return 401 instead of 302

查看:195
本文介绍了对阿贾克斯的看法login_required装饰返回401,而不是302的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在写了一些看法,以Ajax请求我找到应对它有些奇怪的是,login_required装饰总是返回302状态code为未通过身份验证的用户。由于这些意见是阿贾克斯的看法,这似乎有点不合适。我不希望用户在这种情况下登录,但我想Django的告诉客户端身份验证才能访问这样的观点(401应该是正确的状态code,我认为)。

While writing some views to respond to ajax requests i find it somewhat strange that the login_required decorator always returns a 302 status code for not authenticated users. As these views are ajax views, this seems somewhat inappropriate. I do not want the user to log in in such a case, but i want Django to tell the client that authentication is required to access such a view (a 401 should be the right status code, i think).

要做到这一点,我开始写我自己的装饰login_required_ajax,但不知何故,这已经超出了我的技能。这就是我今天来了这么远:

To achieve this, i started to write my own decorator login_required_ajax, but somehow this is beyond my skills. This is what i have come up with so far:

def login_required_ajax(function=None,redirect_field_name=None):
    """
    Just make sure the user is authenticated to access a certain ajax view

    Otherwise return a HttpResponse 401 - authentication required
    instead of the 302 redirect of the original Django decorator
    """
    def _decorator(view_func):
        def _wrapped_view(request, *args, **kwargs):
            if request.user.is_authenticated():
                return view_func(request, *args, **kwargs)
            else:
                return HttpResponse(status=401)

        if function is None:
            return _decorator
        else:
            return _decorator(function)

在一个视图使用这种装饰,我只要我尝试访问任何网页上的网站获得ViewDoesNotExist例外。

When using this decorator on a view, i get a ViewDoesNotExist exception as soon as i try to access any page on the site.

我第一个想到这个问题可能是直接的回报,当用户没有通过验证的Htt的presponse,因为响应对象不是可调用的。但随后的装饰应该工作,只要我不尝试访问的观点有问题,不应该吗?如果这真的是症结所在,我怎么可以写一个装饰一个返回的Htt presponse以401状态code?

I first thought that the problem could be the direct return of an HttpResponse when a user is not authenticated, because a response object is not a callable. But then the decorator should work as long as i do not try to access the view in question, shouldn't it? And if this really is the crux, how can i write a decorator that returns a HttpResponse with a status code of 401?

推荐答案

这是一个pretty的很好的尝试。这里有几个问题,我发现:

That's a pretty good attempt. Here's a couple of problems I spotted:

  1. _Decorator 函数返回 _wrapped_view
  2. 的缩进你的如果功能无块是有点过 - 对 login_required_ajax 函数需要返回装饰功能。
  1. Your _decorator function should return _wrapped_view.
  2. The indentation for your if function is None block is a bit off -- the login_required_ajax function needs to return the decorated function.

下面的这些变化作出的装饰:

Here's the decorator with those changes made:

def login_required_ajax(function=None,redirect_field_name=None):
    """
    Just make sure the user is authenticated to access a certain ajax view

    Otherwise return a HttpResponse 401 - authentication required
    instead of the 302 redirect of the original Django decorator
    """
    def _decorator(view_func):
        def _wrapped_view(request, *args, **kwargs):
            if request.user.is_authenticated():
                return view_func(request, *args, **kwargs)
            else:
                return HttpResponse(status=401)
        return _wrapped_view

    if function is None:
        return _decorator
    else:
        return _decorator(function)

这篇关于对阿贾克斯的看法login_required装饰返回401,而不是302的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆