Reputation: 7762
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?
Upvotes: 10
Views: 7491
Reputation: 31
I had the same error as you described.
My django code is:
>>> response = self.client.get('**/admin**')
>>> self.assertEqual(response.status_code, 200)
AssertionError: 301 != 200
This is my solution:
Option 1
self.client.get('**/admin/**')
Option 2
self.client.get('**/admin**', follow=True)
Upvotes: 3
Reputation: 51
Open your browser to see if the tailing backslash has caused this issue.
Upvotes: 5
Reputation: 1926
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)
Upvotes: 0
Reputation: 196
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.
Upvotes: 18
Reputation: 599600
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.
Upvotes: 1