In get_context_data, you already have the object in self.object (and you can do self.object.pk). Here's what happens up the class hierarchy (DetailView inherits from BaseDetailView):
class BaseDetailView(SingleObjectMixin, View): """ A base view for displaying a single object """ def get(self, request, *args, **kwargs): self.object = self.get_object() context = self.get_context_data(object=self.object) return self.render_to_response(context)
Reading Django source code to understand things is incredibly simple.
And by the way, I'm not sure that you can always rely on the fact that kwargs has the key "pk".
source share