Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django request.user.is_authenticated is always true?

Tags:

python

django

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')
like image 800
zentenk Avatar asked Feb 16 '12 18:02

zentenk


People also ask

What is Is_authenticated in django?

For Django 1.10 + 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.

How do you check whether the user is authenticated or not in django?

Check the Logged in User in Views in Django We can use request. user. is_authenticated to check if the user is logged in or not. If the user is logged in, it will return True .

How do you check if a user is logged in Python?

To check if a user is logged in with Python Django, we can use the request. user. is_authenticated property in our view. to check if the current user is authenticated with request.


3 Answers

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).

like image 96
Richard Green Avatar answered Sep 19 '22 13:09

Richard Green


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/

like image 36
dm03514 Avatar answered Sep 19 '22 13:09

dm03514


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!

like image 20
Benyamin Avatar answered Sep 18 '22 13:09

Benyamin