RequestFactory&用pk反转(必须用对象pk或slug调用)

时间:2017-06-07 18:45:58

标签: django django-testing

所以我有这段代码:

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.

1 个答案:

答案 0 :(得分:3)

您必须使用请求和主键调用视图,例如:

std::sort(std::begin(g1), std::end(g1));