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

Sep 23, 2013 · by Tim Kamanin

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

from 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/ and now you can call your script with ./ yourcommand. Simple!

Example taken from:

Want to get more 🔥 tips like this one?

Subscribe to get notified about new dev tutorials