I’m currently learning some basics of django using tutorials in documentation (Writing your first Django app, part 5 | Django documentation | Django) and have a problem with chapter about testing.
class QuestionDetailViewTests(TestCase):
def test_past_question(self):
question = create_question('text', -30)
response = self.client.get(reverse('polls:detail', args=(question.id,)))
self.assertContains(response, question.question_text)
create_question is just a shortcut for creating Question(models.Model) object with second argument meaning offset in days for publication date, which is set relatively to current date. ‘polls:detail’ is the name of path to this concrete view, which takes number of question as argument.
Running of py manage.py test app results in following error:
Traceback (most recent call last):
File "C:\web\django\project1\mysite\polls\tests.py", line 59, in test_past_question
response = self.client.get(reverse('polls:detail', args=(question.id,)))
File "C:\web\django\project1\lib\site-packages\django\test\client.py", line 927, in get
response = super().get(path, data=data, secure=secure, headers=headers, **extra)
File "C:\web\django\project1\lib\site-packages\django\test\client.py", line 457, in get
return self.generic(
File "C:\web\django\project1\lib\site-packages\django\test\client.py", line 609, in generic
return self.request(**r)
File "C:\web\django\project1\lib\site-packages\django\test\client.py", line 891, in request
self.check_exception(response)
File "C:\web\django\project1\lib\site-packages\django\test\client.py", line 738, in check_exception
raise exc_value
File "C:\web\django\project1\lib\site-packages\django\core\handlers\exception.py", line 55, in inner
response = get_response(request)
File "C:\web\django\project1\lib\site-packages\django\core\handlers\base.py", line 197, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\web\django\project1\lib\site-packages\django\views\generic\base.py", line 104, in view
return self.dispatch(request, *args, **kwargs)
File "C:\web\django\project1\lib\site-packages\django\views\generic\base.py", line 143, in dispatch
return handler(request, *args, **kwargs)
File "C:\web\django\project1\lib\site-packages\django\views\generic\detail.py", line 108, in get
self.object = self.get_object()
File "C:\web\django\project1\lib\site-packages\django\views\generic\detail.py", line 37, in get_object
queryset = queryset.filter(pk=pk)
File "C:\web\django\project1\lib\site-packages\django\db\models\query.py", line 1436, in filter
return self._filter_or_exclude(False, args, kwargs)
File "C:\web\django\project1\lib\site-packages\django\db\models\query.py", line 1448, in _filter_or_exclude
raise TypeError("Cannot filter a query once a slice has been taken.")
TypeError: Cannot filter a query once a slice has been taken.
My code is slightly different from the one posted in tutorial, but it doesn’t work as well. All previous test of views in tutorial work properly, but they do not contain reverse() function