[英]Django 'User' object has no attribute 'user' when combining login_required with user_passes_test
I'm trying to combine a custom user_passes_test
with a login_required
decorator.我正在尝试将自定义
user_passes_test
与login_required
装饰器结合起来。
This works fine:这很好用:
@login_required
@user_passes_test(profile_expired, "Main:profile_expired")
def home(request):
return render(request, "Main/pages/home.html", {})
In helpers.py在 helpers.py 中
def profile_expired(user):
if user.is_authenticated and user.profile.expiration_date is not None:
return user.profile.expiration_date >= date.today()
elif user.profile.expiration_date is None:
return True
return False
If I visit /app/
and profile_expired
returns True, I get redirected to /app/profile_expired
.如果我访问
/app/
并且profile_expired
返回 True,我将被重定向到/app/profile_expired
。
However, the very same code on all other views, is returning error...但是,所有其他视图上的相同代码返回错误...
@login_required
@user_passes_test(profile_expired, "Main:profile_expired")
def another_view(request):
# some code...
return render(request, "Main/pages/another_page.html", context)
Error:错误:
Traceback (most recent call last):
File "C:\Users\S\PycharmProjects\SO\venv\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
response = get_response(request)
File "C:\Users\S\PycharmProjects\SO\venv\lib\site-packages\django\core\handlers\base.py", line 115, in _get_response
response = self.process_exception_by_middleware(e, request)
File "C:\Users\S\PycharmProjects\SO\venv\lib\site-packages\django\core\handlers\base.py", line 113, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\S\PycharmProjects\SO\venv\lib\site-packages\django\contrib\auth\decorators.py", line 21, in _wrapped_view
return view_func(request, *args, **kwargs)
File "C:\Users\S\PycharmProjects\SO\venv\lib\site-packages\django\contrib\auth\decorators.py", line 20, in _wrapped_view
if test_func(request.user):
File "C:\Users\S\PycharmProjects\SO\venv\lib\site-packages\django\contrib\auth\decorators.py", line 20, in _wrapped_view
if test_func(request.user):
File "C:\Users\S\PycharmProjects\SO\venv\lib\site-packages\django\utils\functional.py", line 257, in inner
return func(self._wrapped, *args)
AttributeError: 'User' object has no attribute 'user'
While home
has just the code above, another_view
(and all other views) have more code... But the error cannot be inside the view since I've placed a breakpoint inside of them, started the debugger, and they never get triggered.虽然
home
只有上面的代码,但another_view
(和所有其他视图)有更多代码......但是错误不能在视图内部,因为我在它们内部放置了一个断点,启动了调试器,并且它们永远不会被触发。 The error must be somewhere else.错误必须在其他地方。
I can't pinpoint why is this happening.我无法确定为什么会这样。 Any idea?
任何的想法?
The test_func should take a user
as its argument. test_func 应该以
user
为参数。
def profile_expired(user):
return user.profile.expiry_date > date.today()
You haven't shown your function, but I suspect that you have written it to take a request, then use request.user
.你没有展示你的功能,但我怀疑你已经写了它来接受请求,然后使用
request.user
。
def profile_expired(request):
return request.user.profile.expiry_date > date.today()
Somehow, having the profile_expired
view declared before the views that uses the user_passes_test decorator is what caused the bug.不知何故,在使用 user_passes_test 装饰器的视图之前声明
profile_expired
视图是导致错误的原因。
This will crash another_page
:这将使
another_page
崩溃:
@login_required
@user_passes_test(profile_expired, "Main:profile_expired")
def home(request): #...
@login_required
def profile_expired(request): #...
@login_required
@user_passes_test(profile_expired, "Main:profile_expired")
def another_page(request): #...
Simply moving profile_expired
to the bottom solved the issue.只需将
profile_expired
移动到底部即可解决问题。
I have no idea why and will accept any answer that explains it.我不知道为什么并且会接受任何解释它的答案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.