Reputation: 1835
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
?
Upvotes: 0
Views: 822
Reputation: 308999
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
Upvotes: 1