5

I am using django-registration, and just set it up.

{{user.is_authenticated }}

is true, even though i went already to /accounts/logout/ and logged the user out.

{{user.is_anonymous }} 

returns true also. According to django docs, those two should be different:

is_anonymous: Always returns False. This is a way of differentiating User and AnonymousUser objects. Generally, you should prefer using is_authenticated() to this method.

and

is_authenticated: Always returns True. This is a way to tell if the user has been authenticated. This does not imply any permissions, and doesn't check if the user is active - it only indicates that the user has provided a valid username and password.

I am using the standard views that come with django-registration and haven't touched them yet. In the tempalate i have the following code:

{% if user.is_authenticated %}
{% user }}
{% if user.is_anonymous %}
    is anonymous
{% endif $}
{% else %}
    gotta login
{% endif %}

Where would the problem be? I will be really thankful!

UPDATE: i have noticed that on the homepage, it both is_authenticated and id_anonymous return True, while if i go to /accounts/login before loging in, only is_anonymous returns true as it should be. And also, on the homepage, i have the following view if that helps:

def home(request):
    return render_jinja(request, 'index.html', blah = 'ga')

UPDATE 2: print(request.user.is_authenticated()) gives False. Then, i have:

return render_jinja(request, 'index.html', blah = 'ga')

and in the template, user.is_authenticated returns FALSE.

UPDATE 3: If i use render_to_response, instead of render_jinja, all is good. still don't know how to fix this though :(

mgPePe
  • 5,677
  • 12
  • 52
  • 85
  • I'm afraid I can't reproduce this behaviour at all in a newly created project and app. Which version of Django are you using? How are you logging the user in, and then subsequently logging the user out? – Jarret Hardie Nov 28 '10 at 19:44
  • If you tell me how to check which version of django i have, i will be more than happy to check! shot in the dark - 1.2 – mgPePe Nov 28 '10 at 21:16
  • Since I am using this open source django-registration app, i am logging at the accounts/login/ and logging out at the accounts/logout. and if you tell me how to paste code in comment, i can paste the view from those links – mgPePe Nov 28 '10 at 21:18
  • Here is a snipped from the urls file: url(r'^logout/$', auth_views.logout, {'template_name': 'registration/logout.html'}, name='auth_logout'), – mgPePe Nov 28 '10 at 21:24
  • i found out how to tell django version and result is: (1,2,0, 'beta', 1) – mgPePe Nov 28 '10 at 21:29
  • Thanks @mgPePe. Since Django underwent a few changes to the auth module in 1.2, please try installing the production version of django (Django 1.2 has been out since May 2010, and I note you are still using beta 1), and then re-installing the django-registration app. – Jarret Hardie Nov 28 '10 at 21:36
  • Your tags and variables in the template are incorrect, at least in the above text. You have {% user }} and {% endif $}. Not sure if that would contribute (and likely your actual template doesn't have these errors). – jbaums Nov 28 '10 at 22:11

2 Answers2

11

It seems like you are trying to figure out two things at once; what is the correct way to use jinja templates and what is the deal with User/AnonymousUser. Maybe try to figure out these problems one at a time.

I have no experience with jinja but you may want to check and make sure that you are taking differences between jinja and django template syntax into account. The biggest difference I know of is that a method call requires explicit parenthesis. So the is_authenticated and is_anonymous calls in your example need parenthesis.

Jinja style {{ user.is_authenticated() }}
Django style {{ user.is_authenticated }} 

If that does not solve the problem, try installing django-debug-toolbar and take a look at the context for your template. Check and see if user is None or an object (User or AnonymousUser).

You can also read up on AnonymousUser and see an example of checking for an authenticated user in the docs. In a nutshell, for an AnonymousUser object is_anonymous() always returns True instead of False and is_authenticated() always returns False instead of True.

istruble
  • 13,363
  • 2
  • 47
  • 52
  • Awesome. I thought I solved the problem more or less, but that gives such a nice overview, I will be checking all of those things, gracias! – mgPePe Nov 29 '10 at 08:59
  • Note for future readers: is_authenticated is a property now, so some of the information here is outdated. – wim Jan 12 '17 at 17:49
4

Smack on the head. I read somewhere:

if user.is_authenticated: ....# Always true, since it is a method!

And so, instead of having {{user.is_authenticated}} in template, it should be {{user.is_authenticated()}}

mgPePe
  • 5,677
  • 12
  • 52
  • 85
  • 5
    Not so in Django 1.8. Be careful, as is_authenticated returns a non-callable bool (basically a property, not a function). – Michael Butler Oct 06 '15 at 01:28