DjangoDjango

How to run an arbitrary script in the context of the Django project?

Sep 23, 2013 · Updated: Nov 10, 2017 · by Tim Kamanin

The easiest way to do this is to set up your script as a manage.py subcommand.
Here's how to do that:

python
from django.core.management.base import NoArgsCommand, make_option

class Command(NoArgsCommand):

    help = "Whatever you want to print here"

    option_list = NoArgsCommand.option_list + (
        make_option('--verbose', action='store_true'),
    )

    def handle_noargs(self, **options):
        ... call your script here ...

Save it as management/commands/yourcommand.py and now you can call your script with ./manage.py yourcommand. Simple!

Example taken from: http://stackoverflow.com/a/1310642/469575.

Hey, if you've found this useful, please share the post to help other folks find it:

There's even more:

Subscribe for updates

  • via Twitter: @timonweb
  • old school RSS:
  • or evergreen email ↓ ↓ ↓