Django 测试 client.get() returns 302 代码而不是 200
Django Test client.get() returns 302 code instead of 200
运行 在 url returns 302 而不是 200 上进行测试。但在生产环境中使用重定向测试器 returns 测试相同的 url 200. 不太确定发生了什么。
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', {})
如果我将 follow=True
添加到 client.get()
,我将按预期收到 200 代码。
在测试中 assertEqual
行之前打印 response['location']
的值。它将向您显示客户端被重定向到的位置(例如登录页面)。
def test_detail(self):
response = self.client.get('/p/myproduct-detail.html')
print(response['location'])
self.assertEqual(response.status_code, 200)
运行 在 url returns 302 而不是 200 上进行测试。但在生产环境中使用重定向测试器 returns 测试相同的 url 200. 不太确定发生了什么。
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', {})
如果我将 follow=True
添加到 client.get()
,我将按预期收到 200 代码。
在测试中 assertEqual
行之前打印 response['location']
的值。它将向您显示客户端被重定向到的位置(例如登录页面)。
def test_detail(self):
response = self.client.get('/p/myproduct-detail.html')
print(response['location'])
self.assertEqual(response.status_code, 200)