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 8490565
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T22:09:44+00:00 2026-06-10T22:09:44+00:00

Problem UPDATE : This issue, it turns out, has little to do with the

  • 0

Problem

UPDATE: This issue, it turns out, has little to do with the @login_required decorator!

I am getting finicky behavior when I try to test views that are decorated with @login_required.

I have one test that is actually able to go to a view decorated with @login_required (a password change view). A different test, however, always gets redirected to login. No matter which way I’ve tried to re-write it, it won’t let my test user through, even though I am logging the user in and asserting that user.is_authenticated() beforehand.

Here’s the relevant snippet of the test that’s having issues:

# Log user in
self.client.login(username=user.username, password=user.password)
self.assertTrue(user.is_authenticated())

# Go to account_edit view
url = reverse('account_edit')
response = self.client.get(url)
self.assertEqual(response.status_code, 200)
self.assertTemplateUsed(response, 'users/account_edit_form.html')

This inevitably redirects to the login view, as if the user were not logged in.

I can confirm that this behavior is not reflected in the “normal” functioning of the app; the decorator works exactly as expected when I actually run the server and then navigate to this view (i.e. it allows access when logged in, and redirects to login view).

Using Django 1.3.1. I am using the testrunner from django-nose, but I can confirm that this issue happens regardless of which testrunner I use.

Also, I found several previous questions, but the solutions suggested were either particular to older versions of Django, or not helpful in this case (see here for example).

Solution (combining two good answers)

I received two very good answers to this question, both of which highlighted important oversights in the snippet I posted. The issue had nothing to do with @login_required‘s behavior, and everything to do with (a) signing the user in wrong and (b) checking the user authentication wrong.

I was having a hard time picking which answer to accept, but after a little thought, I’ve decided to accept Konrad Hałas’s answer, since it pinpoints the crucial oversight on my part, that was the root of unexpected behavior.

Nonetheless, I would have figured this out much sooner if I had not been using the faulty test line self.assertTrue(user.is_authenticated()). So to emphasize that the solution was actually two parts, here are the two steps to fixing the problematic code:

# Log user in *NOTE: Password needs to be raw (from Konrad's answer)
self.client.login(username=user.username, password="pass")
self.assertTrue(user.is_authenticated()) # <-- still not correct

The assertion line is still faulty because a valid user always satisfies user.is_authenticated(). See Alasdair’s info for an explanation of this gotcha. So step two of fixing this code is:

# Log user in *NOTE: Password needs to be raw (from Konrad's answer)
login_successful = self.client.login(username=user.username, password="pass")
self.assertTrue(login_successful) # Much better! (see Alasdair's answer)

Finally, should you need to test that your user was logged in without using client.login (i.e. testing a login form), this should work:

self.assertTrue(response.context['user'].is_authenticated())
  • 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-06-10T22:09:46+00:00Added an answer on June 10, 2026 at 10:09 pm

    user.password is password hash. You can’t log in with it. You need to use original password:

    self.client.login(username=user.username, password='<user password>')
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Update II Problem Solved but Why? This has been the biggest headache ever. My
UPDATE It looks like this problem has been quietly fixed in iOS 4.3. Up
Update: turns out the problem is more complicated than I originally thought. I was
Update: This is, as I was told, no principle Python related problem, but seems
I have a problem when trying to execute this update statement (below) using C#
UPDATE: First problem solved, second one described at the bottom of this post. UPDATE2:
[Update] Unfortunately I never had an opportunity to solve this problem. However, there are
Update: After some more reading I see that this problem is totally general, you
UPDATE: After searching a bit I don't seem to be alone about this problem:
I have a problem with the logic of this code, XNA's Update Method is

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.