login_required无效
用django的restful写成的请求处理,使用auth模块中装饰器进行访问限制,,出现request无user属性的错误.
from django.views.generic import Viewfrom django.contrib.auth.decorators import login_required:”’执行中的任务”’:self.manager = TaskQueueManager():”’>> c.get(“/task/queue/”).content”’records = self.manager.list()return {“success”: True, “msg”: “”, “data”: records}:”’>> c.delete(“/task/queue/”, json.dumps({“ids”: [14]})).content”’params = json.loads(request.body)for id in params[“ids”]:self.manager.delete(id)return {“success”: True, “msg”: “”}查找原因
从django里拿到源码,调试后,发现View对象被赋给了装饰器的request.
:”””Decorator for views that checks that the user passes the given test,redirecting to the log-in page if necessary. The test should be a callablethat takes the user object and returns True if the user passes.”””::if test_func(request.user):return view_func(request, *args, **kwargs)path = request.build_absolute_uri()resolved_login_url = resolve_url(login_url or settings.LOGIN_URL)# If the login url is the same scheme and net location then just# use the path as the “next” url.login_scheme, login_netloc = urlparse(resolved_login_url)[:2]current_scheme, current_netloc = urlparse(path)[:2]if ((not login_scheme or login_scheme == current_scheme) and(not login_netloc or login_netloc == current_netloc)):path = request.get_full_path()from django.contrib.auth.views import redirect_to_loginreturn redirect_to_login(path, resolved_login_url, redirect_field_name)return _wrapped_viewreturn decorator:”””Decorator for views that checks that the user is logged in, redirectingto the log-in page if necessary.”””actual_decorator = user_passes_test(lambda u: u.is_authenticated(),login_url=login_url,redirect_field_name=redirect_field_name)if function:return actual_decorator(function)return actual_decorator自定义login_required
解决的办法是进行自定义. 将user_passes_test和login_required拿出来,作如下修改
…:if test_func(request.user):# 加上selfreturn view_func(self, request, *args, **kwargs)…
鸟儿爱美,不仅需要羽毛之美,还需要鸣声婉转之美;