@register.filter
def format_name(user, format='first_last'):
 last_name = (getattr(user, 'last_name', None) or user.get('last_name', None))
 first_name = (getattr(user, 'first_name', None) or user.get('first_name', None))
 username = (getattr(user, 'username', None) or user.get('username', None))
 if (format == 'first_last'):
  if (last_name and first_name):
   return ('%s %s' % (first_name, last_name))
  else:
   return (first_name or last_name or username)
 elif (format == 'last_first'):
  if (last_name and first_name):
   return ('%s, %s' % (last_name, first_name))
  else:
   return (last_name or first_name or username)
 else:
  raise NotImplementedError(('Unrecognized format string: %s' % format))

@ register . filter def format_name ( user , format = 'first_last' ) : last_name = ( getattr ( user , 'last_name' , None ) or user . get ( 'last_name' , None ) ) first_name = ( getattr ( user , 'first_name' , None ) or user . get ( 'first_name' , None ) ) username = ( getattr ( user , 'username' , None ) or user . get ( 'username' , None ) ) if ( format == 'first_last' ) : if ( last_name and first_name ) : return ( '%s %s' % ( first_name , last_name ) ) else : return ( first_name or last_name or username ) elif ( format == 'last_first' ) : if ( last_name and first_name ) : return ( '%s, %s' % ( last_name , first_name ) ) else : return ( last_name or first_name or username ) else : raise NotImplementedError ( ( 'Unrecognized format string: %s' % format ) )
can be used for objects or dictionaries .