我正在尝试将自定义const context = require.context('./',true,/\.spec\.ts$/);
与user_passes_test
装饰器组合在一起。
这很好:
login_required
在helpers.py
@login_required
@user_passes_test(profile_expired,"Main:profile_expired")
def home(request):
return render(request,"Main/pages/home.html",{})
如果我访问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
并且/app/
返回True,我将被重定向到profile_expired
。
但是,所有其他视图上的代码完全相同,正在返回错误...
/app/profile_expired
错误:
@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)
虽然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",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'
仅具有上面的代码,但是home
(和所有其他视图)具有更多的代码...但是错误不能出现在视图内部,因为我已经在其中放置了一个断点,启动了调试器,但它们从未被触发。该错误必须在其他地方。
我无法确定为什么会这样。有想法吗?