Django-Extensions is a nifty little django application that comes with some really useful convenience features. The one I use the most is "runscript." It sets up the Django environment and runs your python script in it so that you don't have to worry about importing all of the right stuff or setting paths or any of that nonsense.
Documentation. Its terrible. In fact, the documentation for runscript doesn't exist on thier documentation site. This guy has attempted to fill in the holes. However, what if you want to pass in some arguments? Crap.
I went digging on github and it turns out there's another basic documentation page, but still no help.
To the source!
We're in luck. They did provide some documentation in the form of a help flag. Running python manage.py runscript --help yields the following:
Options: -v VERBOSITY, --verbosity=VERBOSITY Verbosity level; 0=minimal output, 1=normal output, 2=verbose output, 3=very verbose output --settings=SETTINGS The Python path to a settings module, e.g. "myproject.settings.main". If this isn't provided, the DJANGO_SETTINGS_MODULE environment variable will be used. --pythonpath=PYTHONPATH A directory to add to the Python path, e.g. "/home/djangoprojects/myproject". --traceback Print traceback on exception --fixtures Only look in app.fixtures subdir --noscripts Look in app.scripts subdir -s, --silent Run silently, do not show errors and tracebacks --no-traceback Do not show tracebacks --script-args=SCRIPT_ARGS Space-separated argument list to be passed to the scripts. Note that the same arguments will be passed to all named scripts. --version show program's version number and exit -h, --help show this help message and exit
So to send the script an argument, we tack on a "--script-args" followed by a space separated list of strings. Great!
Now to figure out how to actually use them...
In the source, you can see that the run function is being passed a "*script-args" parameter. So I changed my script to look like this:
def run(*script_args): print script_args
When I run "python manage.py runscript myscript --script-args Testing 123" it prints out
Now you can use the arguments just like you would any other arguments. You just have to be careful when using multiple arguments because they're in a tuple and not an object. That means you can't test to see if a key exists or ensure that the arguments will be entered in any order.