【问题标题】:RequestFactory & reverse with pk (must be called with either an object pk or a slug)RequestFactory & reverse with pk (必须使用对象 pk 或 slug 调用)
【发布时间】:2017-06-07 18:45:57
【问题描述】:

所以我有这个代码:

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.

【问题讨论】:

    标签: django django-testing


    【解决方案1】:

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

    EditAutomation.as_view()(request, pk=self.rule.id)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-07-07
      • 2014-06-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-25
      相关资源
      最近更新 更多