zentenk
zentenk

Reputation: 2815

django request.user.is_authenticated is always true?

Can anyone tell me why in the following code I get redirected to yahoo.com instead of google.com?

urls

urlpatterns = patterns('', (r'^$', initialRequest,))

view

def initialRequest(request):

    if request.user.is_authenticated:
        return HttpResponseRedirect('http://yahoo.com')
    else:
        return HttpResponseRedirect('http://google.com')

Upvotes: 18

Views: 43904

Answers (4)

I've django 2.0, I tried this and works

if request.user.is_authenticated:

Upvotes: 2

Benyamin
Benyamin

Reputation: 410

its changed again from Pull request #216.

now your problem is fixed, if you are using Django 2.0+, look at this GitHub issue is the same issues you had. so in Django 2.0+

request.user.is_authenticated

is true!

Upvotes: 4

Richard Green
Richard Green

Reputation: 2062

Shouldn't it be request.user.is_authenticated() i.e. with brackets as it's a function?

For Django 1.10 +

is_authenticated is now an attribute (although it is being kept backwards compatible for now).

Upvotes: 51

dm03514
dm03514

Reputation: 55922

As Richard mentioned is_authenticated is a function, so in your view it should be called like: request.user.is_authenticated().

Because of django templating language there can be confusion, because calling this in a template makes it appear as a property and not a method.

{{ user.is_authenticated}} https://docs.djangoproject.com/en/dev/topics/auth/

Upvotes: 12

Related Questions