# 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:
class UserView(DetailView):
template_name = 'template.html'
#model = User
#context_object_name = 'foo'
def get_object(self):
return get_object_or_404(User, pk=request.session['user_id'])
# 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:
# {{ user.id }}
# You can override this name by using context_object_name.
# https://chriskief.com/2012/12/29/django-generic-detailview-without-a-pk-or-slug/