[Django]-Why is django.test.client.Client not keeping me logged in

0👍

FWIW, an update to Django 1.2 (I was running 1.1.1 before) fixed it. I have no idea what was broken there, considering when I last ran that test suite about 2 weeks ago, it worked great.

👤Fred

1👍

Just happened to me when retesting an app that has been working and forgotten for some months.

The solution (apart from updating to Django 1.2) is Patch #11821. In short, Python 2.6.5 has some bugfix in the Cookie module, triggering an edge case bug in the test client.

👤Javier

0👍

I use RequestContext to get the logged in user into the template context.

from django.shortcuts import render_to_response
from django.contrib.auth.decorators import login_required
from django.template import RequestContext

@login_required
def index(request):
   return render_to_response('page.html',
                             {},
                             context_instance=RequestContext(request))

and in the template

{% if user.is_authenticated %} ... {{ user.username }} .. {% endif %}

This works as expected (I don’t get to this page without logging in, and when I get there, the username is present in response.content) when driven through the test client.

Leave a comment