Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django Test client.get() returns 302 code instead of 200

Running a test on a url returns 302 instead of 200. Yet testing the same urls in production with a redirect tester returns 200. Not really sure what's going on.

tests.py

def test_detail(self):
    response = self.client.get('/p/myproduct-detail.html')
    self.assertEqual(response.status_code, 200)

urls.py

    url(r'^p/(?P<slug>[-\w\d]+).html$', main.views.product_detail, 
        name='product-detail'),

views.py

def product_detail(request, slug):
    stuff...
    return render(request, 'product-detail.html', {})

If I add follow=True to client.get() I receive 200 code as expected.

like image 220
KingFu Avatar asked Dec 07 '17 13:12

KingFu


1 Answers

Print the value of response['location'] in your test before your assertEqual line. It will show you where the client is being redirected to (e.g. the login page).

def test_detail(self):
    response = self.client.get('/p/myproduct-detail.html')
    print(response['location'])
    self.assertEqual(response.status_code, 200)
like image 197
Alasdair Avatar answered Oct 17 '22 00:10

Alasdair