user.is_authenticated is always false using default login

93 views
Skip to first unread message

Andrew English

unread,
May 20, 2008, 11:32:33 AM5/20/08
to django...@googlegroups.com
My view function uses the login required decorator:

@login_required(redirect_field_name='next')
def index(request):
   lists = List.objects.filter(user=request.user)
   return render_to_response('base.html', {'lists': lists})


This directs them to the login page:
LOGIN_URL = '/todone/list/login/'
LOGIN_REDIRECT_URL = '/todone/list/'


Which is correctly handled by the django auth default:
(r'^todone/list/login/$', 'django.contrib.auth.views.login'),

I am using the default template from the authentication instructions:

{% extends "base.html" %}

{% block content %}

{% if form.has_errors %}
<p>Your username and password didn't match. Please try again.</p>
{% endif %}

<form method="post" action=".">
<table>
<tr><td><label for="id_username">Username:</label></td><td>{{ form.username }}</td></tr>
<tr><td><label for="id_password">Password:</label></td><td>{{ form.password }}</td></tr>
</table>

<input type="submit" value="login" />
<input type="hidden" name="next" value="{{ next }}" />
</form>

{% endblock %}


When the user logins in with correct credentials, they are redirected to the expected page.  However in the template, user.is_authenticated is false and user.username is empty.  But in the views.py, lists = List.objects.filter(user=request.user) will return the expected results.  Even though the user is not authenticated, the request.user object has the expected values. 

Do I need to explicitly call authenticate and login in my own view to populate the user data?  From what I read, it seems that the django.contrib.auth.views.login does that automatically.

Thanks.

James Bennett

unread,
May 20, 2008, 11:51:51 AM5/20/08
to django...@googlegroups.com
On Tue, May 20, 2008 at 10:32 AM, Andrew English
<andrew....@gmail.com> wrote:
> Do I need to explicitly call authenticate and login in my own view to
> populate the user data? From what I read, it seems that the
> django.contrib.auth.views.login does that automatically.

There's a difference between authenticating the user, which you've
done, and making the user object available as a variable to the
template, which I'm guessing you haven't done. You probably want to
read this:

http://www.djangoproject.com/documentation/templates_python/#subclassing-context-requestcontext


--
"Bureaucrat Conrad, you are technically correct -- the best kind of correct."

Reply all
Reply to author
Forward
0 new messages