Fixed #18545 -- Make the 'no DJANGO_SETTINGS_MODULE' error message more useful.Thanks Nick Coghlan for the report, and Malcolm Tredinnick for review.

This commit is contained in:
Carl Meyer 2012-09-08 14:12:18 -06:00
parent c26dd4f154
commit 307706d082
3 changed files with 34 additions and 24 deletions

View File

@ -26,7 +26,7 @@ class LazySettings(LazyObject):
The user can manually configure settings prior to using them. Otherwise, The user can manually configure settings prior to using them. Otherwise,
Django uses the settings module pointed to by DJANGO_SETTINGS_MODULE. Django uses the settings module pointed to by DJANGO_SETTINGS_MODULE.
""" """
def _setup(self): def _setup(self, name):
""" """
Load the settings module pointed to by the environment variable. This Load the settings module pointed to by the environment variable. This
is used the first time we need any settings at all, if the user has not is used the first time we need any settings at all, if the user has not
@ -37,12 +37,21 @@ class LazySettings(LazyObject):
if not settings_module: # If it's set but is an empty string. if not settings_module: # If it's set but is an empty string.
raise KeyError raise KeyError
except KeyError: except KeyError:
# NOTE: This is arguably an EnvironmentError, but that causes raise ImproperlyConfigured(
# problems with Python's interactive help. "Requested setting %s, but settings are not configured. "
raise ImportError("Settings cannot be imported, because environment variable %s is undefined." % ENVIRONMENT_VARIABLE) "You must either define the environment variable %s "
"or call settings.configure() before accessing settings."
% (name, ENVIRONMENT_VARIABLE))
self._wrapped = Settings(settings_module) self._wrapped = Settings(settings_module)
def __getattr__(self, name):
if self._wrapped is empty:
self._setup(name)
return getattr(self._wrapped, name)
def configure(self, default_settings=global_settings, **options): def configure(self, default_settings=global_settings, **options):
""" """
Called to manually configure the settings. The 'default_settings' Called to manually configure the settings. The 'default_settings'

View File

@ -5,6 +5,7 @@ from optparse import OptionParser, NO_DEFAULT
import imp import imp
import warnings import warnings
from django.core.exceptions import ImproperlyConfigured
from django.core.management.base import BaseCommand, CommandError, handle_default_options from django.core.management.base import BaseCommand, CommandError, handle_default_options
from django.core.management.color import color_style from django.core.management.color import color_style
from django.utils.importlib import import_module from django.utils.importlib import import_module
@ -105,7 +106,7 @@ def get_commands():
try: try:
from django.conf import settings from django.conf import settings
apps = settings.INSTALLED_APPS apps = settings.INSTALLED_APPS
except (AttributeError, EnvironmentError, ImportError): except (AttributeError, ImproperlyConfigured):
apps = [] apps = []
# Find and load the management module for each installed app. # Find and load the management module for each installed app.

View File

@ -181,11 +181,11 @@ class DjangoAdminNoSettings(AdminScriptTestCase):
"A series of tests for django-admin.py when there is no settings.py file." "A series of tests for django-admin.py when there is no settings.py file."
def test_builtin_command(self): def test_builtin_command(self):
"no settings: django-admin builtin commands fail with an import error when no settings provided" "no settings: django-admin builtin commands fail with an error when no settings provided"
args = ['sqlall', 'admin_scripts'] args = ['sqlall', 'admin_scripts']
out, err = self.run_django_admin(args) out, err = self.run_django_admin(args)
self.assertNoOutput(out) self.assertNoOutput(out)
self.assertOutput(err, 'environment variable DJANGO_SETTINGS_MODULE is undefined') self.assertOutput(err, 'settings are not configured')
def test_builtin_with_bad_settings(self): def test_builtin_with_bad_settings(self):
"no settings: django-admin builtin commands fail if settings file (from argument) doesn't exist" "no settings: django-admin builtin commands fail if settings file (from argument) doesn't exist"
@ -213,11 +213,11 @@ class DjangoAdminDefaultSettings(AdminScriptTestCase):
self.remove_settings('settings.py') self.remove_settings('settings.py')
def test_builtin_command(self): def test_builtin_command(self):
"default: django-admin builtin commands fail with an import error when no settings provided" "default: django-admin builtin commands fail with an error when no settings provided"
args = ['sqlall', 'admin_scripts'] args = ['sqlall', 'admin_scripts']
out, err = self.run_django_admin(args) out, err = self.run_django_admin(args)
self.assertNoOutput(out) self.assertNoOutput(out)
self.assertOutput(err, 'environment variable DJANGO_SETTINGS_MODULE is undefined') self.assertOutput(err, 'settings are not configured')
def test_builtin_with_settings(self): def test_builtin_with_settings(self):
"default: django-admin builtin commands succeed if settings are provided as argument" "default: django-admin builtin commands succeed if settings are provided as argument"
@ -279,11 +279,11 @@ class DjangoAdminFullPathDefaultSettings(AdminScriptTestCase):
self.remove_settings('settings.py') self.remove_settings('settings.py')
def test_builtin_command(self): def test_builtin_command(self):
"fulldefault: django-admin builtin commands fail with an import error when no settings provided" "fulldefault: django-admin builtin commands fail with an error when no settings provided"
args = ['sqlall', 'admin_scripts'] args = ['sqlall', 'admin_scripts']
out, err = self.run_django_admin(args) out, err = self.run_django_admin(args)
self.assertNoOutput(out) self.assertNoOutput(out)
self.assertOutput(err, 'environment variable DJANGO_SETTINGS_MODULE is undefined') self.assertOutput(err, 'settings are not configured')
def test_builtin_with_settings(self): def test_builtin_with_settings(self):
"fulldefault: django-admin builtin commands succeed if a settings file is provided" "fulldefault: django-admin builtin commands succeed if a settings file is provided"
@ -345,11 +345,11 @@ class DjangoAdminMinimalSettings(AdminScriptTestCase):
self.remove_settings('settings.py') self.remove_settings('settings.py')
def test_builtin_command(self): def test_builtin_command(self):
"minimal: django-admin builtin commands fail with an import error when no settings provided" "minimal: django-admin builtin commands fail with an error when no settings provided"
args = ['sqlall', 'admin_scripts'] args = ['sqlall', 'admin_scripts']
out, err = self.run_django_admin(args) out, err = self.run_django_admin(args)
self.assertNoOutput(out) self.assertNoOutput(out)
self.assertOutput(err, 'environment variable DJANGO_SETTINGS_MODULE is undefined') self.assertOutput(err, 'settings are not configured')
def test_builtin_with_settings(self): def test_builtin_with_settings(self):
"minimal: django-admin builtin commands fail if settings are provided as argument" "minimal: django-admin builtin commands fail if settings are provided as argument"
@ -411,11 +411,11 @@ class DjangoAdminAlternateSettings(AdminScriptTestCase):
self.remove_settings('alternate_settings.py') self.remove_settings('alternate_settings.py')
def test_builtin_command(self): def test_builtin_command(self):
"alternate: django-admin builtin commands fail with an import error when no settings provided" "alternate: django-admin builtin commands fail with an error when no settings provided"
args = ['sqlall', 'admin_scripts'] args = ['sqlall', 'admin_scripts']
out, err = self.run_django_admin(args) out, err = self.run_django_admin(args)
self.assertNoOutput(out) self.assertNoOutput(out)
self.assertOutput(err, 'environment variable DJANGO_SETTINGS_MODULE is undefined') self.assertOutput(err, 'settings are not configured')
def test_builtin_with_settings(self): def test_builtin_with_settings(self):
"alternate: django-admin builtin commands succeed if settings are provided as argument" "alternate: django-admin builtin commands succeed if settings are provided as argument"
@ -482,11 +482,11 @@ class DjangoAdminMultipleSettings(AdminScriptTestCase):
self.remove_settings('alternate_settings.py') self.remove_settings('alternate_settings.py')
def test_builtin_command(self): def test_builtin_command(self):
"alternate: django-admin builtin commands fail with an import error when no settings provided" "alternate: django-admin builtin commands fail with an error when no settings provided"
args = ['sqlall', 'admin_scripts'] args = ['sqlall', 'admin_scripts']
out, err = self.run_django_admin(args) out, err = self.run_django_admin(args)
self.assertNoOutput(out) self.assertNoOutput(out)
self.assertOutput(err, 'environment variable DJANGO_SETTINGS_MODULE is undefined') self.assertOutput(err, 'settings are not configured')
def test_builtin_with_settings(self): def test_builtin_with_settings(self):
"alternate: django-admin builtin commands succeed if settings are provided as argument" "alternate: django-admin builtin commands succeed if settings are provided as argument"
@ -570,11 +570,11 @@ class DjangoAdminSettingsDirectory(AdminScriptTestCase):
self.assertTrue(os.path.exists(os.path.join(app_path, 'api.py'))) self.assertTrue(os.path.exists(os.path.join(app_path, 'api.py')))
def test_builtin_command(self): def test_builtin_command(self):
"directory: django-admin builtin commands fail with an import error when no settings provided" "directory: django-admin builtin commands fail with an error when no settings provided"
args = ['sqlall', 'admin_scripts'] args = ['sqlall', 'admin_scripts']
out, err = self.run_django_admin(args) out, err = self.run_django_admin(args)
self.assertNoOutput(out) self.assertNoOutput(out)
self.assertOutput(err, 'environment variable DJANGO_SETTINGS_MODULE is undefined') self.assertOutput(err, 'settings are not configured')
def test_builtin_with_bad_settings(self): def test_builtin_with_bad_settings(self):
"directory: django-admin builtin commands fail if settings file (from argument) doesn't exist" "directory: django-admin builtin commands fail if settings file (from argument) doesn't exist"
@ -621,7 +621,7 @@ class ManageNoSettings(AdminScriptTestCase):
"A series of tests for manage.py when there is no settings.py file." "A series of tests for manage.py when there is no settings.py file."
def test_builtin_command(self): def test_builtin_command(self):
"no settings: manage.py builtin commands fail with an import error when no settings provided" "no settings: manage.py builtin commands fail with an error when no settings provided"
args = ['sqlall', 'admin_scripts'] args = ['sqlall', 'admin_scripts']
out, err = self.run_manage(args) out, err = self.run_manage(args)
self.assertNoOutput(out) self.assertNoOutput(out)
@ -786,7 +786,7 @@ class ManageMinimalSettings(AdminScriptTestCase):
self.remove_settings('settings.py') self.remove_settings('settings.py')
def test_builtin_command(self): def test_builtin_command(self):
"minimal: manage.py builtin commands fail with an import error when no settings provided" "minimal: manage.py builtin commands fail with an error when no settings provided"
args = ['sqlall', 'admin_scripts'] args = ['sqlall', 'admin_scripts']
out, err = self.run_manage(args) out, err = self.run_manage(args)
self.assertNoOutput(out) self.assertNoOutput(out)
@ -852,7 +852,7 @@ class ManageAlternateSettings(AdminScriptTestCase):
self.remove_settings('alternate_settings.py') self.remove_settings('alternate_settings.py')
def test_builtin_command(self): def test_builtin_command(self):
"alternate: manage.py builtin commands fail with an import error when no default settings provided" "alternate: manage.py builtin commands fail with an error when no default settings provided"
args = ['sqlall', 'admin_scripts'] args = ['sqlall', 'admin_scripts']
out, err = self.run_manage(args) out, err = self.run_manage(args)
self.assertNoOutput(out) self.assertNoOutput(out)
@ -895,7 +895,7 @@ class ManageAlternateSettings(AdminScriptTestCase):
args = ['noargs_command'] args = ['noargs_command']
out, err = self.run_manage(args) out, err = self.run_manage(args)
self.assertNoOutput(out) self.assertNoOutput(out)
self.assertOutput(err, "Unknown command: 'noargs_command'") self.assertOutput(err, "Could not import settings 'regressiontests.settings'")
def test_custom_command_with_settings(self): def test_custom_command_with_settings(self):
"alternate: manage.py can execute user commands if settings are provided as argument" "alternate: manage.py can execute user commands if settings are provided as argument"
@ -927,7 +927,7 @@ class ManageMultipleSettings(AdminScriptTestCase):
self.remove_settings('alternate_settings.py') self.remove_settings('alternate_settings.py')
def test_builtin_command(self): def test_builtin_command(self):
"multiple: manage.py builtin commands fail with an import error when no settings provided" "multiple: manage.py builtin commands fail with an error when no settings provided"
args = ['sqlall', 'admin_scripts'] args = ['sqlall', 'admin_scripts']
out, err = self.run_manage(args) out, err = self.run_manage(args)
self.assertNoOutput(out) self.assertNoOutput(out)