October, 2010

03
Oct 10

Django HttpResponseRedirect returns 301 instead of 302

If you have some code that redirects like this…

def index(request):
    return HttpResponseRedirect('/widget/create')

And you want to test it with assertRedirects() like this…

def test_widget_redirect(self)
    response = self.client.get('/')
    self.assertRedirects(response, '/widget/create')

And you can’t figure out why your test fails like this…

AssertionError: Couldn't retrieve redirection page '/widget/create': response 
    code was 301 (expected 200)

Then you should read this.

In short, add a trailing slash to your url.

'/widget/create/'