├── README.md └── start-my-django.py /README.md: -------------------------------------------------------------------------------- 1 | # start-my-django 2 | A script that starts your django project in correct way i.e starts your virtualenv, starts django project, starts app and also initializes git. 3 | Forget the pain of doing all of these steps one-by-one manually.The script does everything for you 4 | 5 | 6 | Requirements: 7 | 8 | 1) Python (2.7 or greater) 9 | 2) VirtualEnv (pip install virtualenv) 10 | 11 | How to run the script: 12 | 13 | 1) Create the folder in which you would like to store your django project (mkdir /path/to/folder) 14 | and navigate to that folder 15 | 2) Copy the 'start-my-django.py' file into that folder 16 | 3) Then run python start-my-django.py 17 | 18 | What script does: 19 | 20 | 1) When you run the script, it will first install and activate virtual env in the current folder 21 | 2) It will then prompt you to install django. If you wish to proceed, then it installs Django 22 | 3) Once django installation is done, it asks for the django project name and starts the project 23 | 4) After the project is started, it asks for app name and starts the app 24 | 5) Once the app is started, it will ask if you wish to do git initialization. If yes, 25 | then it initializes git in the current directory. 26 | 6) It will create the .gitignore file for you which will ignore all the environment related directories and all *.pyc files. 27 | 7) After this it will make the first git commit 28 | 29 | You can use the script to start any number of django projects. 30 | 31 | I have tested script against bare minimum combinations of bad-inputs. Please report the issues incase the script fails 32 | due to bad inputs 33 | 34 | -------------------------------------------------------------------------------- /start-my-django.py: -------------------------------------------------------------------------------- 1 | import os 2 | import subprocess 3 | import re 4 | import sys 5 | yes = re.compile("yes|Y|y|Yes") 6 | 7 | 8 | 9 | def install_django(virtualenv_directory): 10 | try: 11 | print "Installing django....." 12 | subprocess.call(['{0}/bin/pip'.format(virtualenv_directory), "install","django"]) 13 | except Exception,e: 14 | print "Unable to Install Django.",e 15 | exit() 16 | 17 | 18 | def get_project_name(): 19 | print '******************************************** \n' 20 | django_project_name = raw_input("Please enter the django project name. Please use only numbers, letters and underscores. \n") 21 | return django_project_name 22 | 23 | 24 | def start_project(django_project_name, virtualenv_directory): 25 | print "Starting django project....." 26 | try: 27 | command_run = subprocess.call(["{0}/bin/django-admin.py".format(virtualenv_directory), "startproject", django_project_name]) 28 | except CommandError as e: 29 | command_run = 1 #random digit assignment to make the below condition false 30 | if command_run == 0: 31 | print "Project Successfully Started" 32 | return True 33 | else: 34 | return 35 | 36 | 37 | def start_app(django_project_name, virtualenv_directory): 38 | print '******************************************** \n' 39 | django_app_name = raw_input("Please enter the App name. The app name should be different from project name. Please use only numbers, letters and underscores. \n") 40 | os.chdir(django_project_name) 41 | os.getcwd() 42 | try: 43 | command_run = subprocess.call(["../{0}/bin/python".format(virtualenv_directory), "manage.py", "startapp", django_app_name]) 44 | except Exception as e: 45 | command_run = 1 46 | if command_run == 0: 47 | print "App Successfully Started" 48 | else: 49 | os.chdir('../') 50 | start_app(django_project_name, virtualenv_directory) 51 | 52 | 53 | def start_git(): 54 | print '******************************************** \n' 55 | print "Before you start writing code, you should know that, \nYour life will be difficult if you do not use Version Control System. Start using Git." 56 | git_confirm = raw_input("Do you want to go ahead with git initialization(yes/no) \n") 57 | if yes.match(git_confirm): 58 | try: 59 | os.chdir('../') 60 | subprocess.call(["touch",".gitignore"]) 61 | with open(".gitignore", "w") as f: 62 | f.write("bin/\nlib/\nlocal/\ninclude/\n*.pyc") 63 | subprocess.call(["git", "init"]) 64 | subprocess.call(["git", "add", "."]) 65 | subprocess.call(["git", "commit", "-m", "first commit"]) 66 | print "\nGreat work buddy. Git iniitalised and first commit done. You are now safe under the wings of git" 67 | except Exception,e: 68 | print "Couldnt initialise Git",e 69 | exit() 70 | print "\nGreat work buddy. You can code now." 71 | 72 | 73 | def start_virtualenv(): 74 | confirm = raw_input("Welcome to installation. First up, lets install virtualenv.\n Do you want to install in current directory(yes/no) \n") 75 | confirm = yes.match(confirm) 76 | if confirm: 77 | virtualenv_directory = '.' 78 | else: 79 | virtualenv_directory = raw_input("Please Enter a directory name \n") 80 | 81 | command_run = 1 82 | while(command_run != 0): 83 | py_version = raw_input("Which Python version do you want to use(2/3) \n") 84 | py_path = "python" + py_version 85 | 86 | try: 87 | command_run = subprocess.call(["virtualenv", "-p", py_path, virtualenv_directory,]) 88 | except OSError, ex: 89 | print "Something went wrong with the directory path.Please check your directory path",ex 90 | sys.exit() 91 | 92 | if command_run != 0: 93 | print '\nPlease, enter only 2 or 3 for Python version. \n' 94 | 95 | print "\nAwesome. VirtualEnv is installed." 96 | return virtualenv_directory 97 | 98 | 99 | def main(): 100 | virtualenv_directory = start_virtualenv() 101 | print '******************************************** \n' 102 | 103 | confirm = raw_input("Do you want to continue with Django installation(yes/no) \n") 104 | confirm = yes.match(confirm) 105 | 106 | project_success = False 107 | if confirm: 108 | install_django(virtualenv_directory) 109 | while(not project_success): 110 | project_name = get_project_name() 111 | project_success = start_project(project_name, virtualenv_directory) 112 | start_app(project_name, virtualenv_directory) 113 | start_git() 114 | 115 | 116 | #start everything 117 | main() --------------------------------------------------------------------------------