Ticket #15064: 15064_DJANGO_SETTINGS_MODULE_ignored.diff

File 15064_DJANGO_SETTINGS_MODULE_ignored.diff, 2.8 KB (added by ShawnMilo, 4 years ago)

Patch with test. If environment variable DJANGO_SETTINGS_MODULE is set it will be used.

  • django/core/management/__init__.py

    diff --git django/core/management/__init__.py django/core/management/__init__.py
    index edc36f4..ec1c0d1 100644
    def setup_environ(settings_mod, original_settings_path=None): 
    411411    if original_settings_path:
    412412        os.environ['DJANGO_SETTINGS_MODULE'] = original_settings_path
    413413    else:
    414         os.environ['DJANGO_SETTINGS_MODULE'] = '%s.%s' % (project_name, settings_name)
     414        #If DJANGO_SETTINGS_MODULE is already set, use it.
     415        os.environ['DJANGO_SETTINGS_MODULE'] = os.environ.get(
     416            'DJANGO_SETTINGS_MODULE',
     417            '%s.%s' % (project_name, settings_name)
     418        )
    415419
    416420    # Import the project module. We add the parent directory to PYTHONPATH to
    417421    # avoid some of the path errors new users can have.
  • tests/regressiontests/settings_tests/tests.py

    diff --git tests/regressiontests/settings_tests/tests.py tests/regressiontests/settings_tests/tests.py
    index 5a4e8a7..11ac240 100644
    class TrailingSlashURLTests(unittest.TestCase): 
    7676        self.settings_module.MEDIA_URL = 'http://media.foo.com/stupid//'
    7777        self.assertEqual('http://media.foo.com/stupid//',
    7878                         self.settings_module.MEDIA_URL)
     79
     80
     81class EnvironmentVariableTest(unittest.TestCase):
     82
     83    """
     84    Ensures proper settings file is used.
     85    """
     86
     87    def test_env_var_used(self):
     88
     89        """
     90        If the environment variable is set, do
     91        not ignore it. However, the kwarg
     92        original_settings_path takes precedence.
     93
     94        This tests both plus the default (neither set).
     95        """
     96
     97        import os
     98        from django.core.management import setup_environ
     99
     100        #whatever was already there
     101        default_module =  os.environ.get(
     102            'DJANGO_SETTINGS_MODULE',
     103            'the default'
     104        )
     105
     106        #environment variable set by user
     107        user_override = 'custom.settings'
     108
     109        #optional argument to setup_environ
     110        orig_path = 'original.path'
     111
     112        #expect default
     113        setup_environ(global_settings)
     114        self.assertEquals(
     115            os.environ.get('DJANGO_SETTINGS_MODULE'),
     116            default_module
     117        )
     118
     119        #override with environment variable
     120        os.environ['DJANGO_SETTINGS_MODULE'] = user_override
     121        setup_environ(global_settings)
     122
     123        self.assertEquals(
     124            os.environ.get('DJANGO_SETTINGS_MODULE'),
     125            user_override
     126        )
     127
     128        #pass in original_settings_path (takes precedence)
     129        os.environ['DJANGO_SETTINGS_MODULE'] = user_override
     130        setup_environ(global_settings, original_settings_path = orig_path)
     131
     132        self.assertEquals(
     133            os.environ.get('DJANGO_SETTINGS_MODULE'),
     134            orig_path
     135        )
     136
Back to Top