Use Django to start the command line and execute the script

  • 2020-10-23 21:03:45
  • OfStack

Using django to start the command line and script, you can easily use the django framework for development, such as database operations.

The usage is described below.

The launch of django shell

Start command:


$/data/python-virtualenv/apple/bin/python /data/example/apple/manage.py shell

How is it different from the regular python command line?

django shell comes with django configuration information and can use django's framework. For example, by defining model as Apples, you can query all records directly using Apples.objects.all ().


> a=Apples.objects.all()
> print a

The script

The path of the script must be:


app_name/management/commands

For example, create a script for an apple reference with the following path:


/data/example/apple/management/commands/AutoCheckTicket.py

The code is as follows:


# -*- coding: utf-8 -*-

from django.core.management.base import BaseCommand
from monitor import ticket_monitor

class Command(BaseCommand):
 def handle(self, *args, **options):
  print "gogo"
  ticket_monitor.main_entry()

Execution mode:


$/data/python-virtualenv/apple/bin/python /data/example/apple/manage.py AutoCheckTicket

Related articles: