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:
parent
c26dd4f154
commit
307706d082
|
@ -26,7 +26,7 @@ class LazySettings(LazyObject):
|
|||
The user can manually configure settings prior to using them. Otherwise,
|
||||
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
|
||||
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.
|
||||
raise KeyError
|
||||
except KeyError:
|
||||
# NOTE: This is arguably an EnvironmentError, but that causes
|
||||
# problems with Python's interactive help.
|
||||
raise ImportError("Settings cannot be imported, because environment variable %s is undefined." % ENVIRONMENT_VARIABLE)
|
||||
raise ImproperlyConfigured(
|
||||
"Requested setting %s, but settings are not configured. "
|
||||
"You must either define the environment variable %s "
|
||||
"or call settings.configure() before accessing settings."
|
||||
% (name, ENVIRONMENT_VARIABLE))
|
||||
|
||||
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):
|
||||
"""
|
||||
Called to manually configure the settings. The 'default_settings'
|
||||
|
|
|
@ -5,6 +5,7 @@ from optparse import OptionParser, NO_DEFAULT
|
|||
import imp
|
||||
import warnings
|
||||
|
||||
from django.core.exceptions import ImproperlyConfigured
|
||||
from django.core.management.base import BaseCommand, CommandError, handle_default_options
|
||||
from django.core.management.color import color_style
|
||||
from django.utils.importlib import import_module
|
||||
|
@ -105,7 +106,7 @@ def get_commands():
|
|||
try:
|
||||
from django.conf import settings
|
||||
apps = settings.INSTALLED_APPS
|
||||
except (AttributeError, EnvironmentError, ImportError):
|
||||
except (AttributeError, ImproperlyConfigured):
|
||||
apps = []
|
||||
|
||||
# Find and load the management module for each installed app.
|
||||
|
|
|
@ -181,11 +181,11 @@ class DjangoAdminNoSettings(AdminScriptTestCase):
|
|||
"A series of tests for django-admin.py when there is no settings.py file."
|
||||
|
||||
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']
|
||||
out, err = self.run_django_admin(args)
|
||||
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):
|
||||
"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')
|
||||
|
||||
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']
|
||||
out, err = self.run_django_admin(args)
|
||||
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):
|
||||
"default: django-admin builtin commands succeed if settings are provided as argument"
|
||||
|
@ -279,11 +279,11 @@ class DjangoAdminFullPathDefaultSettings(AdminScriptTestCase):
|
|||
self.remove_settings('settings.py')
|
||||
|
||||
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']
|
||||
out, err = self.run_django_admin(args)
|
||||
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):
|
||||
"fulldefault: django-admin builtin commands succeed if a settings file is provided"
|
||||
|
@ -345,11 +345,11 @@ class DjangoAdminMinimalSettings(AdminScriptTestCase):
|
|||
self.remove_settings('settings.py')
|
||||
|
||||
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']
|
||||
out, err = self.run_django_admin(args)
|
||||
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):
|
||||
"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')
|
||||
|
||||
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']
|
||||
out, err = self.run_django_admin(args)
|
||||
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):
|
||||
"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')
|
||||
|
||||
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']
|
||||
out, err = self.run_django_admin(args)
|
||||
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):
|
||||
"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')))
|
||||
|
||||
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']
|
||||
out, err = self.run_django_admin(args)
|
||||
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):
|
||||
"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."
|
||||
|
||||
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']
|
||||
out, err = self.run_manage(args)
|
||||
self.assertNoOutput(out)
|
||||
|
@ -786,7 +786,7 @@ class ManageMinimalSettings(AdminScriptTestCase):
|
|||
self.remove_settings('settings.py')
|
||||
|
||||
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']
|
||||
out, err = self.run_manage(args)
|
||||
self.assertNoOutput(out)
|
||||
|
@ -852,7 +852,7 @@ class ManageAlternateSettings(AdminScriptTestCase):
|
|||
self.remove_settings('alternate_settings.py')
|
||||
|
||||
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']
|
||||
out, err = self.run_manage(args)
|
||||
self.assertNoOutput(out)
|
||||
|
@ -895,7 +895,7 @@ class ManageAlternateSettings(AdminScriptTestCase):
|
|||
args = ['noargs_command']
|
||||
out, err = self.run_manage(args)
|
||||
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):
|
||||
"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')
|
||||
|
||||
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']
|
||||
out, err = self.run_manage(args)
|
||||
self.assertNoOutput(out)
|
||||
|
|
Loading…
Reference in New Issue