[Answered ]-Django CBV AttributeError: Generic detail view must be called with either an object pk or a slug

2👍

What you’re doing here is calling the view directly, rather than going through Django’s URL resolver/dispatcher. So you need to pass the arguments the view is expecting: in this case, as the error message says, including the slug/pk. So:

response = item_view(request, item.slug)

However, a much better way to test views is to use the built-in test client object, which means you can get rid of all of that code and replace it with just this:

response = self.client.get(item.get_absolute_url())

Leave a comment