MiniGunnR
MiniGunnR

Reputation: 5800

Why does AnonymousUser returns True for is_authenticated in Django?

I have the following code in my view file.

if request.user.is_authenticated:
    owner = request.user
    if cart.owner != owner:
        cart.owner = owner
    cart.save()

However I face the following error:

Cannot assign "<SimpleLazyObject: <django.contrib.auth.models.AnonymousUser object at 0x103ff37b8>>": "Cart.owner" must be a "User" instance.

Why is Django running the line cart.owner = owner even if the user is not authenticated?

Upvotes: 0

Views: 443

Answers (1)

Sayse
Sayse

Reputation: 43300

is_authenticated is a method, you're just checking to see if there is a method available with that name, not whether your user is authenticated.

You need to call the method.

if request.user.is_authenticated():

Upvotes: 4

Related Questions