Opened 9 years ago

Closed 9 years ago

#25285 closed Cleanup/optimization (fixed)

Unknown django-admin command raises "settings not configured" when no project exists

Reported by: Maxime Lorant Owned by: nobody
Component: Core (Management commands) Version: dev
Severity: Normal Keywords: django-admin command
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

When you try to execute a command that does not exist outside a Django project (e.g. createproject instead of startproject...), Django raise a ImproperlyConfigured that does not fit with the real error:

(testdjango):~/testdjango$ django-admin.py not-a-real-command test
Traceback (most recent call last):
  File "/root/testdjango/bin/django-admin.py", line 5, in <module>
    management.execute_from_command_line()
  File "/root/testdjango/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 338, in execute_from_command_line
    utility.execute()
  File "/root/testdjango/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 330, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/root/testdjango/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 182, in fetch_command
    settings.INSTALLED_APPS
  File "/root/testdjango/local/lib/python2.7/site-packages/django/conf/__init__.py", line 48, in __getattr__
    self._setup(name)
  File "/root/testdjango/local/lib/python2.7/site-packages/django/conf/__init__.py", line 42, in _setup
    % (desc, ENVIRONMENT_VARIABLE))
django.core.exceptions.ImproperlyConfigured: Requested setting INSTALLED_APPS, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.

The expected output is:

(testdjango):~/testdjango$ django-admin.py not-a-real-command test
Unknown command: 'not-a-real-command'
Type 'django-admin.py help' for usage.

Attachments (1)

unknown-command-exception.patch (695 bytes ) - added by Maxime Lorant 9 years ago.
Suggestion of patch

Download all attachments as: .zip

Change History (6)

by Maxime Lorant, 9 years ago

Suggestion of patch

comment:1 by Tim Graham, 9 years ago

Easy pickings: unset
Summary: Unknown command raise the wrong exception when no project existUnknown django-admin command raises "settings not configured" when no project exists
Triage Stage: UnreviewedAccepted

I think your proposal makes sense. It keeps the fix for #21634, but only when using manage.py. Claude, what do you think?

comment:2 by Claude Paroz, 9 years ago

comment:3 by Maxime Lorant, 9 years ago

Indeed, as a Django user, I would also prefer this implementation: it provides the correct error message and give a hint in case the user tried to use a custom command. (Also, it avoids hardcoded filename, like "django-admin" in the code...)

Thanks Claude.

comment:4 by Tim Graham, 9 years ago

Triage Stage: AcceptedReady for checkin

comment:5 by Claude Paroz <claude@…>, 9 years ago

Resolution: fixed
Status: newclosed

In c1893e2:

Fixed #25285 -- Provided unknown command message with plain django-admin.py

Thanks Maxime Lorant for the report and Tim Graham for suggesting
the improved comment.

Note: See TracTickets for help on using tickets.
Back to Top