code: def get_object(context):
      
      object = None
  
      view = context.get('view')
      if view:
          # View is more reliable then an 'object' variable in the context.
          # Works if this is a SingleObjectMixin
          object = getattr(view, 'object', None)
  
      if object is None:
          object = context.get('object', None)
  
      return object
docs: Get an object from the context or view.