By default the generic DetailView expects you to provide a pk or slug in the URL. If it’s missing you’ll get a lovely little error:
Generic detail view UserView must be called with either an object pk or a slug
For the most part this is fine as you’ll usually provide an identifier in the URL. But what if you need to pull the identifier from someplace else such as the session?
To accomplish this, simply override the get_object method:
Also note that the model is no longer necessary as you’re explicitly calling it in get_object. The object is now available in the template using the model name, in this case user:
You can override this name by using context_object_name.