Have you tried all the steps recommended by Apple?
Use target view mode
Follow these steps to start using your iMac as a screen:
1 Make sure your iMac is turned on and the other Mac is connected to a macOS user account. 2 Connect the Mini DisplayPort or Thunderbolt cable between the two computers. 3 Press Command-F2 on the keyboard of the iMac you want to use as the screen.
After pressing Command-F2, your iMac displays the desktop from the connected Mac.
If Command-F2 does not work
If your iMac does not switch to target display mode when you press Command-F2, check the following:
• Make sure that the iMac you are trying to use as the display supports the target display mode. • The target display mode only works when your main Mac is connected to a macOS user account. For example, if your first Mac is at the FileVault login screen, pressing Command-F2 on your iMac does not activate the target display mode. You must first connect to your Mac (with the help of a normal screen, if necessary) before you can use an iMac in target display mode. • Make sure to press Command-F2 on the keyboard connected to the iMac you want to use as the screen. • In Keyboard System Preferences, if the checkbox is enabled for "Use all F1, F2, etc., as standard function keys", the key combination becomes Command-Fn-F2. • Make sure your Mini DisplayPort or Thunderbolt cable is connected to both computers. • If you are currently connected to the iMac that you want to use as a display, try disconnecting to return to the login window. Then press Command-F2 again. • Some older Apple keyboards and third-party keyboards may not allow Command-F2 to switch between display modes. If this happens, use the keyboard that came with your iMac to enable or disable the target display mode. • If your iMac is currently booted on Windows, it will not go into target display mode. Target display mode is not supported in Boot Camp.