dkoug Blog

Python script to create a django project

Python -- Posted on Feb. 13, 2019

Python script to create a django project

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import os
import argparse


if __name__ == "__main__":
    ap = argparse.ArgumentParser()
    ap.add_argument("-n", "--name", required=True,
                    help="name of the project")
    args = vars(ap.parse_args())
    name = args["name"]
    path = os.getcwd()
    directory = "{}/sites/{}".format(path, name)
    if not os.path.exists(directory):
        os.mkdir(directory)
    os.chdir(directory)
    if not os.path.exists('public'):
        os.mkdir('public')
    os.system("""python3 -m venv env &&
              . {0}/env/bin/activate &&
              pip install django django-debug-toolbar django-extensions &&
              pip freeze > requirements.txt &&
              django-admin startproject {1} &&
              cd {1} &&
              mkdir static &&
              mkdir templates &&
              cd templates &&
              touch base.html &&
              touch index.html""".format(directory, name))