Django management program

From TRCCompSci - AQA Computer Science
Revision as of 07:45, 2 May 2019 by Admin (talk | contribs) (Management Program)
Jump to: navigation, search

Running and using django in the college environment can be difficult. So i have written this program to perform the key django functions:

Management Program

import os,sys

def djangocommand(option):
    DJANGO_PATH = ""
    for p in sys.path:
        if p.find('Scripts') != -1:
            DJANGO_PATH = p[0:p.find('Scripts')]+"Scripts\python "+p[0:p.find('Scripts')]+"Scripts\django-admin.py"
    os.system(DJANGO_PATH + " "+option)

def managecommand(option):
    MANAGE_PATH = ""
    for p in sys.path:
        if p.find('Scripts') != -1:
            MANAGE_PATH = p[0:p.find('Scripts')]+"Scripts\python "
    MANAGE_PATH +=os.path.dirname(os.path.realpath(__file__))+ "\manage.py"+" "+option
    print(MANAGE_PATH)
    os.system(MANAGE_PATH)

print('Do you want to use:')
print('1: Django-admin')
print('2: Manage')
manoption=input("Please Select 1 or 2:")
if manoption=='1':
    print('What django-admin command do you want to run:')
    print('1: StartProject')
    print('2: StartApp')
    option = input("Please Select 1 or 2:")
    if option=='1':
        name = input("Please enter a project name:")
        djangocommand("startproject "+name+" .")
    elif option=='2':
       name = input("Please enter a app name:")
       djangocommand("startapp "+name)
    else:
        print("invalid option, please re-run")
elif manoption=='2':
    print('What django-admin command do you want to run:')
    print('1: Runserver')
    print('2: MakeMigrations')
    print('3: Migrate')
    print('4: Create Super User')
    option = input("Please Select 1, 2, 3 or 4:")
    if option=='1':
        managecommand("runserver")
    elif option=='2':
       managecommand("makemigrations")
    elif option=='3':
       managecommand("migrate")
    elif option=='4':
       managecommand("createsuperuser")
    else:
        print("invalid option, please re-run")
else:
    print("invalid option, please re-run")

Create a new '.py' file and call it something like 'admin.py', and you can run this by right clicking your new file in solution explorer and then 'start without debugging'.

How to Use

New Project

  • Run the management program and choose '1' as your first option to run django_admin
  • Now selected option 1 to 'StartProject'
  • Now enter a project name, eg 'MyProject'

New App

  • Run the management program and choose '1' as your first option to run django_admin
  • Now selected option 1 to 'StartApp'
  • Now enter a app name, eg 'MyApp'

Run Server

  • Run the management program and choose '2' as your first option to run manage
  • Now selected option 1 to 'RunServer'

Make Migrations

  • Run the management program and choose '2' as your first option to run manage
  • Now selected option 2 to 'MakeMigrations'

Migrate

  • Run the management program and choose '2' as your first option to run manage
  • Now selected option 3 to 'Migrate'