RequestFactory & reverse with pk(必须用对象 pk 或 slug 调用)
RequestFactory & reverse with pk (must be called with either an object pk or a slug)
所以我有这个代码:
request = self.factory.get(reverse('portal-edit-automation', args=(self.rule.id,)))
response = EditAutomation.as_view()(request)
如果我在它之前设置一个 pdb 断点并执行:
(Pdb) reverse('portal-edit-automation', args=(self.rule.id,))
u'/portal/automations/edit/1/'
我得到了预期的回应。
为什么我在 运行 测试套件时得到这个?
AttributeError: Generic detail view EditAutomation must be called with either an object pk or a slug.
您必须使用请求和主键调用视图,例如:
EditAutomation.as_view()(request, pk=self.rule.id)
所以我有这个代码:
request = self.factory.get(reverse('portal-edit-automation', args=(self.rule.id,)))
response = EditAutomation.as_view()(request)
如果我在它之前设置一个 pdb 断点并执行:
(Pdb) reverse('portal-edit-automation', args=(self.rule.id,))
u'/portal/automations/edit/1/'
我得到了预期的回应。
为什么我在 运行 测试套件时得到这个?
AttributeError: Generic detail view EditAutomation must be called with either an object pk or a slug.
您必须使用请求和主键调用视图,例如:
EditAutomation.as_view()(request, pk=self.rule.id)