[1.7.x] Fixed #22699 -- Configure default settings in some management commands.

This makes it possible to run django.setup() in management commands that
don't need a settings module. In addition it simplifies error handling.

Thanks Claude for the review.

Backport of 4865326f from master.