0

I want to add --env=prod/beta/alpha/dev to runserver so i can do python manage.py runserver --env=prod for example.

I've tried to add to settings.py:

parser = CommandParser(None)
parser.add_argument('--env')
parser.add_argument('args', nargs='*')  # catch-all

try:
    options, args = parser.parse_known_args(sys.argv[2:])
except CommandError:
    pass  # Ignore any option errors at this point.

APP_ENV = options.env or 'dev' 

But I got:

usage: manage.py runserver [-h] [--version] [-v {0,1,2,3}]
                       [--settings SETTINGS] [--pythonpath PYTHONPATH]
                       [--traceback] [--no-color] [--ipv6] [--nothreading]
                       [--noreload] [--nostatic] [--insecure]
                       [addrport]
manage.py runserver: error: unrecognized arguments: --env=beta

Any idea how can I monkey patch runserver?

1

1 Answer 1

1

To modify runserver, you could create a custom management command. You should be able to subclass runserver and add the extra arguments.

You might find it easier to set an environment variable, rather than changing the runserver command. In your settings, you would do something like:

import os
env = os.getenv('ENV') or 'dev'

Then you would run the dev server with

ENV=dev python manage.py runserver
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.