'AnonymousUser' object is not iterable

后端 未结 2 1546
暖寄归人
暖寄归人 2021-02-19 20:29
if not request.user.is_authenticated:
    return None

try:
    return ClientProfile.objects.get(user=request.user)
except ClientProfile.DoesNotExist:
    return None
         


        
2条回答
  •  有刺的猬
    2021-02-19 21:11

    In Django 1.9 and earlier, is_authenticated() is a method, you must call it.

    if not request.user.is_authenticated():
        ...
    

    It's an easy mistake to forget to call the method. In your case it's causing an error, but in other cases it might allow users to have access to data that they shouldn't. From Django 1.10, is_authenticated is changing to a property to prevent this.

提交回复
热议问题