django manage.py command code example

Example 1: django add custom commands to manage.py

# To implement the command, edit polls/management/commands/closepoll.py to look like this:
from django.core.management.base import BaseCommand, CommandError
from polls.models import Question as Poll

class Command(BaseCommand):
    help = 'Closes the specified poll for voting'

    def add_arguments(self, parser):
        parser.add_argument('poll_ids', nargs='+', type=int)

    def handle(self, *args, **options):
        for poll_id in options['poll_ids']:
            try:
                poll = Poll.objects.get(pk=poll_id)
            except Poll.DoesNotExist:
                raise CommandError('Poll "%s" does not exist' % poll_id)

            poll.opened = False
            poll.save()

            self.stdout.write(self.style.SUCCESS('Successfully closed poll "%s"' % poll_id))

 # polls/
 #   __init__.py
 #   models.py
 #   management/
 #       commands/
 #           _private.py
 #           closepoll.py
 #   tests.py
 #   views.py

Example 2: django runserver

python manage.py runserver

Example 3: manage.py

It is your tool for executing many Django-specific tasks such as
 starting a new app within a project, running the development server, 
 running your tests etc.
 Example: python manage.py runserver

Example 4: django-admin startproject

$ django-admin <command> [options]
$ manage.py <command> [options]
$ python -m django <command> [options]

Tags:

Misc Example