python - 将 login_required 与 user_passes_test 组合时,Django 'User' 对象没有属性 'user'

标签 python django decorator django-authentication

我正在尝试将自定义 user_passes_testlogin_required 装饰器结合起来。

这很好用:

@login_required
@user_passes_test(profile_expired, "Main:profile_expired")
def home(request):
    return render(request, "Main/pages/home.html", {})

在 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

如果我访问 /app/ 并且 profile_expired 返回 True,我将被重定向到 /app/profile_expired

但是,所有其他 View 上的相同代码返回错误...

@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", 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'

虽然 home 只有上面的代码,another_view(和所有其他 View )有更多代码......但是错误不能在 View 内部,因为我已经在它们里面放置一个断点,启动调试器,它们永远不会被触发。错误必须在其他地方。

我无法确定为什么会这样。有什么想法吗?

最佳答案

test_func 应该以user 作为参数。

def profile_expired(user):
    return user.profile.expiry_date > date.today()

你没有展示你的功能,但我怀疑你已经写了它来接受请求,然后使用 request.user

def profile_expired(request):
    return request.user.profile.expiry_date > date.today()

关于python - 将 login_required 与 user_passes_test 组合时,Django 'User' 对象没有属性 'user',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59054577/

相关文章:

jquery - 使用 jquery 将模板变量传递给 django url

Python - 在装饰器中获取原始函数参数

javascript - 如何避免对此进行硬编码?在装饰器中

python - 如何获取多个峰值下的面积值

Python - 在文本中搜索出现

python - 如何使用 Python 查找并列出 2 个给定月份之间的月份

python - django.db.models.fields.related_descriptors.RelatedObjectDoesNotExist 通过测试 django 形式

django - Wagtail 管理站点和 Django 管理站点之间的区别?

python - python中多个函数的异常处理

python - 每当我进行 OOP 时,“超出最大递归深度”