diff --git a/django/core/management/base.py b/django/core/management/base.py index 910b43ecd29..7b62d6c259a 100644 --- a/django/core/management/base.py +++ b/django/core/management/base.py @@ -386,7 +386,6 @@ class BaseCommand(object): if options.get('no_color'): self.style = no_style() self.stderr = OutputWrapper(options.get('stderr', sys.stderr)) - os.environ[str("DJANGO_COLORS")] = str("nocolor") else: self.stderr = OutputWrapper(options.get('stderr', sys.stderr), self.style.ERROR) diff --git a/django/core/management/commands/runserver.py b/django/core/management/commands/runserver.py index 58011b67b72..28b52e31aca 100644 --- a/django/core/management/commands/runserver.py +++ b/django/core/management/commands/runserver.py @@ -41,6 +41,14 @@ class Command(BaseCommand): parser.add_argument('--noreload', action='store_false', dest='use_reloader', default=True, help='Tells Django to NOT use the auto-reloader.') + def execute(self, *args, **options): + if options.get('no_color'): + # We rely on the environment because it's currently the only + # way to reach WSGIRequestHandler. This seems an acceptable + # compromise considering `runserver` runs indefinitely. + os.environ[str("DJANGO_COLORS")] = str("nocolor") + super(Command, self).execute(*args, **options) + def get_handler(self, *args, **options): """ Returns the default WSGI handler for the runner. diff --git a/tests/admin_scripts/tests.py b/tests/admin_scripts/tests.py index b5eeac713bf..2b3f4824fd6 100644 --- a/tests/admin_scripts/tests.py +++ b/tests/admin_scripts/tests.py @@ -1397,7 +1397,6 @@ class CommandTypes(AdminScriptTestCase): out = StringIO() call_command('color_command', no_color=True, stdout=out) - self.assertEqual(os.environ.get('DJANGO_COLORS', ''), 'nocolor') self.assertEqual(out.getvalue(), 'BEGIN\n') def test_base_command(self):