kaybrian
kaybrian

Reputation: 71

how to make " request.user.is_authenticated" work without throwing an error

i have tried to use the authentication of a user but every time i use it i get a type error telling me that the 'bool' object is not callable. I can't get over it.

 if request.user.is_authenticated():
        print(user)
    else:
        return redirect("/login")
    return render(request, 'home.html', context)

Upvotes: 1

Views: 352

Answers (2)

Navid Khan
Navid Khan

Reputation: 1169

When you receive an error saying x is not callable it's usually because you're trying to call something that is not a function. For instance,

myVar = True
myVar()

would return the same error as above.

In your case, request.user.is_authenticated() needs to be replaced with request.user.is_authenticated ( minus the brackets ) since request.user.is_authenticated is an attribute, not a function.

Cheers!

Upvotes: 1

JPG
JPG

Reputation: 88569

In newer versions of Django, the is_authenticated becomes a attribute rather instead of function.

So use request.user.is_authenticated (with out the paranthesis "()" )

#Code snippet
if request.user.is_authenticated: # change is here <<<<<<<
        print(user)
    else:
        return redirect("/login")
    return render(request, 'h/code>

Upvotes: 2

Related Questions