这是一个很好的尝试。这是我发现的几个问题:
- 您的
_decorator
函数应该返回_wrapped_view
。 - 您的代码
if function is None
块的缩进有点小-该login_required_ajax
函数需要返回修饰后的函数。
这是进行了这些更改的装饰器:
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)



