views:

91

answers:

1

I am looking over: http://docs.djangoproject.com/en/1.2/topics/auth/#django.contrib.auth.models.User but just can't seem to figure out how to do this as its not working. I need to check if the current site user is logged in (authenticated), and am trying:

request.user.is_authenticated

despite being sure that the user is logged in, it returns just:

>

I'm able to do other requests (from the first section in the url above), such as:

request.user.is_active

which returns a successful response. Any advice is appreciated

+4  A: 

is_authenticated is a function. You should call it like

if request.user.is_authenticated():
    # do something if the user is authenticated

As Peter Rowell pointed out, what may be tripping you up is that in the default Django template language, you don't tack on parenthesis to call functions. So you may have seen something like this in template code:

{% if user.is_authenticated %}

However, in Python code, it is indeed a method in the User class.

Brian Neal
oh ok.. thanks for the info, that makes sense then why it wasn't working, unless I missed something, it is really not clear about this in the django documentation
Rick
@Rick: I beg to differ with you. is_authenticated() is the second item listed in the *methods* section of class models.User. What may be confusing is that the *template language* does *not* use the trailing ()'s, so you might see something like {% if user.is_authenticated %}. You'll get an error if you put the ()'s in. (See http://docs.djangoproject.com/en/dev/topics/auth/#django.contrib.auth.models.User.is_authenticated and http://docs.djangoproject.com/en/1.2/topics/templates/#variables)
Peter Rowell
@Peter, well they don't use () in the examples, I realize that I am sure they explained somewhere that its a method and how to do it properly, its just nice when an API uses real life syntax in it so that it can be quickly taken in by someone new to a project like Django, just a pet peeve I guess as I tend to skim through things but I realize I should have looked closer, thanks for the help
Rick
@Rick: I completely agree with you about real life syntax. I have heard the (what I consider) lame reasons they have for not using a "real" programming language for the template system, but that's what they did. You can choose to use Jinja2 (http://jinja.pocoo.org/2/) and it will give you full Python capabilities, but since the overwhelming majority of 3rd party apps use the Django system it is often hard to intermix them. Look at ExprTag (http://djangosnippets.org/snippets/9/) for a way to get expressions inside of Django templates. It works.
Peter Rowell