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?
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With