django – get user logged into test client

The test client is request-agnostic. It doesn’t inherently hold information about what users are logged in. (Neither does your actual webserver or the Django dev server, either, for obvious reasons, and those same reasons apply here).

login is simply a convenience method on the test client to essentially mimic a POST to /login/ with the defined user credentials. Nothing more.

The actual user is available on the request just like in a view. However, since you don’t have direct access to the view, Django makes request available on the view’s response. After you actually use the test client to load a view, you can store the result and then get the user via:


More recent versions of Django will use:


Leave a Comment