Difference between revisions of "Django management program"

From TRCCompSci - AQA Computer Science
Jump to: navigation, search
(Created page with "Running and using django in the college environment can be difficult. So i have written this program to perform the key django functions: <syntaxhighlight lang=python> import...")
 
 
(6 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
Running and using django in the college environment can be difficult. So i have written this program to perform the key django functions:
 
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=
 
<syntaxhighlight lang=python>
 
<syntaxhighlight lang=python>
 
import os,sys
 
import os,sys
Line 12: Line 13:
  
 
def managecommand(option):
 
def managecommand(option):
 +
    MANAGE_PATH = ""
 
     for p in sys.path:
 
     for p in sys.path:
 
         if p.find('Scripts') != -1:
 
         if p.find('Scripts') != -1:
Line 41: Line 43:
 
     print('2: MakeMigrations')
 
     print('2: MakeMigrations')
 
     print('3: Migrate')
 
     print('3: Migrate')
     option = input("Please Select 1, 2 or 3:")
+
    print('4: Create Super User')
 +
     option = input("Please Select 1, 2, 3 or 4:")
 
     if option=='1':
 
     if option=='1':
 
         managecommand("runserver")
 
         managecommand("runserver")
 
     elif option=='2':
 
     elif option=='2':
 
       managecommand("makemigrations")
 
       managecommand("makemigrations")
     elif option=='2':
+
     elif option=='3':
 
       managecommand("migrate")
 
       managecommand("migrate")
 +
    elif option=='4':
 +
      managecommand("createsuperuser")
 
     else:
 
     else:
 
         print("invalid option, please re-run")
 
         print("invalid option, please re-run")
Line 53: Line 58:
 
     print("invalid option, please re-run")
 
     print("invalid option, please re-run")
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
 +
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'
 +
 +
===Create Super User===
 +
* Run the management program and choose '2' as your first option to run manage
 +
* Now selected option 4 to 'CreateSuperUser'

Latest revision as of 10:51, 2 May 2019

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'

Create Super User

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