Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7504843
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T21:42:31+00:00 2026-05-29T21:42:31+00:00

In a written django test, how can I get the current logged in user?

  • 0

In a written django test, how can I get the current logged in user?

For example this is the test I want to write:

def test_author_set_once(self):
    self.client.login(username='Adam', password='password')
    #create an object and test the author is adam
    self.client.login(username='Barry', password='password')
    #modify the first object and test that the author has not changed

So I want to be able to say something like…

self.assertEqual(object.author, self.client.user)

(but I can’t)

The way I’ve coded it at the moment is like this:

self.client.login(username='Adam', password='password')
self.user = User.objects.get(username='Adam')
#create an object 
self.assertEqual(object.author, self.user)

This relies on the assumption that the request.user is the same as a particular user object. I guess it’s OK but it seems a bit clunky.

Is there anyway to avoid the assumption?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T21:42:32+00:00Added an answer on May 29, 2026 at 9:42 pm

    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:

    response.request.user
    

    More recent versions of Django will use:

    response.wsgi_request.user
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've written an auction system in Django. I want to write unit tests but
I've written an entire app pretty successfully in Django but I have this nagging
I've written a simple django app to test ImageField, but I'm running into problem
I have written a small unit test for my django view .My project structure
I've written a custom application which, like django-tagging , can be integrated well with
I'm using Django 1.0.2. I've written a ModelForm backed by a Model. This model
Does anyone know how this could properly be written in Django? {{ mu.expiry_date|default:{% trans
I have written a Django app that makes use of Python threading to create
I have written a django page that requires only super users to login. So
I had the following idea: Say we have a webapp written using django which

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.