这是在tests.py
中class QuizDetailView(TestCase):
def test_quiz_detail_has_html(self):
x = 1 # initial quiz
path = reverse('quiz:detail', args=(x,))
print('\n** quiz:detail = ', path)
print('** HttpResponse = ', HttpResponse(path))
print('-- self.client.get = ', self.client.get(path))
print('-- self.client.get /quiz/1 = ', self.client.get('/quiz/1'))
print('-- self.client.get /quiz/1/ = ', self.client.get('/quiz/1/'))
这是输出
$ python manage.py test
Creating test database for alias 'default'...
System check identified no issues (0 silenced).
.
** quiz:detail = /quiz/1/
** HttpResponse = <HttpResponse status_code=200, "text/html; charset=utf-8">
-- self.client.get = <HttpResponseNotFound status_code=404, "text/html">
-- self.client.get /quiz/1 = <HttpResponsePermanentRedirect status_code=301, "text/html; charset=utf-8", url="/quiz/1/">
-- self.client.get /quiz/1/ = <HttpResponseNotFound status_code=404, "text/html">
因此,HttpResponse可以正常工作,但是self.client为什么不能正常工作?如果我手动转到该网页,则可以正常工作。