gpt4 book ai didi

TypeError "Cannot filter a query once a slice has been taken." while running self.client.get(reverse()) function in django(TypeError“一旦获取切片,就无法过滤查询。”在Django中运行self.client.get(verse())函数时)

转载 作者:bug小助手 更新时间:2023-10-26 19:55:08 27 4
gpt4 key购买 nike



I'm currently learning some basics of django using tutorials in documentation (https://docs.djangoproject.com/en/4.2/intro/tutorial05/) and have a problem with chapter about testing.

我目前正在使用Documentation(https://docs.djangoproject.com/en/4.2/intro/tutorial05/)中的教程学习Django的一些基础知识,并且在测试章节中遇到了问题。


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.

create_question只是创建Question(models.Model)对象的一个快捷方式,该对象带有第二个参数,表示发布日期的偏移量(以天为单位),该偏移量相对于当前日期设置。'polls:detail'是指向这个具体视图的路径的名称,它将问题的数量作为参数。


Running of py manage.py test app results in following error:

运行py Manage.py测试应用程序会导致以下错误:


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

我的代码与教程中发布的代码略有不同,但它不能很好地工作。教程中以前的所有视图测试都可以正常工作,但它们不包含Reverse()函数


更多回答
优秀答案推荐
更多回答

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com