def reverse_version(version):
 if version:
  try:
   return reverse('version-detail', kwargs={'pk': version.pk})
  except AttributeError:
   return version
 return

def reverse_version ( version ) : if version : try : return reverse ( 'version-detail' , kwargs = { 'pk' : version . pk } ) except AttributeError : return version return
the try/except attributeerror allows this to be used where the input is ambiguous .