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

23 Sep · by Tim Kamanin · 1 min read

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

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.

Comments

Required for comment verification