Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fixed #18545 -- Make the 'no DJANGO_SETTINGS_MODULE' error message mo…

…re useful.Thanks Nick Coghlan for the report, and Malcolm Tredinnick for review.
  • Loading branch information...
commit 307706d082d20ac868654ccbaee18879db5197db 1 parent c26dd4f
@carljm carljm authored
View
17 django/conf/__init__.py
@@ -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):

@carljm Adding the argument breaks the diffsettings command, as it calls _setup() to force load all the settings.

Look at line 19:
https://github.com/django/django/blob/e70170c2cbc8871f3bc1f585601b831eef0be9dc/django/core/management/commands/diffsettings.py#L19

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
"""
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 @@ def _setup(self):
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'
View
3  django/core/management/__init__.py
@@ -5,6 +5,7 @@
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.
View
38 tests/regressiontests/admin_scripts/tests.py
@@ -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 @@ def tearDown(self):
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 @@ def tearDown(self):
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 @@ def tearDown(self):
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 @@ def tearDown(self):
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 @@ def tearDown(self):
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 @@ def test_setup_environ_custom_template(self):
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 @@ def tearDown(self):
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 @@ def tearDown(self):
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 @@ def test_custom_command(self):
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 @@ def tearDown(self):
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)

0 comments on commit 307706d

Please sign in to comment.
Something went wrong with that request. Please try again.