Question

I know there are few same posts with this problem, but they doesn't helped for me. I'm always got a 301 status in tests:

self.client.get('/')

and this:

self.client.get('/admin/')

return:

AssertionError: 301 != 200

All urls will returning 301 status... Only way that help is: self.client.get('/', follow=True)

Anybody knows where is problem?

Was it helpful?

Solution

301 is status for redirection, whitch means your get request first have response that is the 301. Http headers contains the url to redirect to...

If you want your request to follow, you have pass in follow=True, which indicates the method to automatically trigger another request to the redirect url. There can be many redirections.

It's a common error in assertion tests.

OTHER TIPS

Open your browser to see if the tailing backslash has caused this issue.

I had the same error as you described.

My django code is:

response = self.client.get('**/admin**')<br>
self.assertEqual(response.status_code, 200)<br>
AssertionError: 301 != 200

This is my solution:
Option 1

self.client.get('**/admin/**')<br>

Option 2

self.client.get('**/admin**', follow=True) 

Is the root URL protected by login? That's certainly the case for the admin URL, so it will redirect to the login page unless you have already logged in. If you have protected the root view with @login_required that would explain what you see.

This is how I solved it:

    def test_index_status_code(self):
        response = self.client.get('/backstage')
        self.assertRedirects(response, '/backstage/', status_code=301, target_status_code=200)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top