在views.py中的urls.py中包装/装饰函数

时间:2022-03-06 20:28:43

So, I'm pretty familiar with wrapping functions in views.py. So I've written a decorator to redirect to the default REDIRECT_URL if the user is logged in (sort of a reverse login_required); it's based on how I've made views in the past:

所以,我非常熟悉在views.py中包装函数。所以我写了一个装饰器,如果用户已登录,则重定向到默认的REDIRECT_URL(反向登录是一个反向登录);这是基于我过去的观点:

def not_logged_in(redirect_url=None, redirect_field_name=REDIRECT_FIELD_NAME):
    def decorator(view_func, *args, **kwargs):
        def wrapper(request, *args, **kwargs):
            if not request.user.is_authenticated():
                return view_func(*args, **kwargs)
            else:
                redirect_url = (request.REQUEST.get(redirect_field_name, redirect_url) or
                                settings.REDIRECT_URL)
                return HttpResponseRedirect(redirect_url)
        return wrapper
    return decorator

However, I get the following error: 'function' object has no attribute 'status_code' which is caused by a MiddleWare expecting an HttpResponse. When I look at the value for response, I see that it's <function wrapper at 0x2b3a9922a500>.

但是,我收到以下错误:'function'对象没有属性'status_code',这是由MiddleWare期望HttpResponse引起的。当我查看响应的值时,我看到它是

Here's how I'm calling it in urls.py:

这是我在urls.py中调用它的方式:

url(r'login/', 
     not_logged_in(auth_views.login), 
     {'authentication_form': LoginForm },
),

3 个解决方案

#1


8  

Here’s my implementation of the same thing.

这是我对同一件事的实现。

def logout_required(view):
    def f(request, *args, **kwargs):
        if request.user.is_anonymous():
            return view(request, *args, **kwargs)
        return HttpResponseRedirect(settings.LOGIN_REDIRECT_URL)
    return f

In urls.py:

urlpatterns = patterns("",
    url(r"^login/", logout_required(login), {"template_name": "users/login.html"}, "login"),
    # ...

I hope this helps (unsure though).

我希望这有助于(不确定)。

#2


1  

The first argument to a decorator should be the function that needs to be decorated.

装饰器的第一个参数应该是需要装饰的函数。

def not_logged_in(func, redirect_url=None, redirect_field_name=REDIRECT_FIELD_NAME):

The decorator function is also not needed. Return the wrapper function from not_logged_in.

装饰器功能也是不需要的。从not_logged_in返回包装函数。

#3


1  

The way you've implemented your decorator, it is parameterised and therefore callable: that's why you've got the extra level of function that fizixx wrongly says is not required. You need to call the outer wrapper initially, in order to return the actual decorated function. So something like:

你实现装饰器的方式,它是参数化的,因此可以调用:这就是为什么你得到了fizixx错误地说不需要的额外功能。您最初需要调用外部包装器,以便返回实际的修饰函数。所以类似于:

url(r'login/', 
 not_logged_in(auth_views.login)('/redirect/', 'redirect_field'), 
 {'authentication_form': LoginForm },
),

#1


8  

Here’s my implementation of the same thing.

这是我对同一件事的实现。

def logout_required(view):
    def f(request, *args, **kwargs):
        if request.user.is_anonymous():
            return view(request, *args, **kwargs)
        return HttpResponseRedirect(settings.LOGIN_REDIRECT_URL)
    return f

In urls.py:

urlpatterns = patterns("",
    url(r"^login/", logout_required(login), {"template_name": "users/login.html"}, "login"),
    # ...

I hope this helps (unsure though).

我希望这有助于(不确定)。

#2


1  

The first argument to a decorator should be the function that needs to be decorated.

装饰器的第一个参数应该是需要装饰的函数。

def not_logged_in(func, redirect_url=None, redirect_field_name=REDIRECT_FIELD_NAME):

The decorator function is also not needed. Return the wrapper function from not_logged_in.

装饰器功能也是不需要的。从not_logged_in返回包装函数。

#3


1  

The way you've implemented your decorator, it is parameterised and therefore callable: that's why you've got the extra level of function that fizixx wrongly says is not required. You need to call the outer wrapper initially, in order to return the actual decorated function. So something like:

你实现装饰器的方式,它是参数化的,因此可以调用:这就是为什么你得到了fizixx错误地说不需要的额外功能。您最初需要调用外部包装器,以便返回实际的修饰函数。所以类似于:

url(r'login/', 
 not_logged_in(auth_views.login)('/redirect/', 'redirect_field'), 
 {'authentication_form': LoginForm },
),