[Fixed]-How to invoke a script which creates django app and project from views.py

1👍

I don’t know what’s your intention to make only 3 lines of code to be in a script unless you run the script somewhere else, but I think you could just move them inside your views.py. You can always make several lines of code to be a function:

In views.py

import os

def create_project:
    os.chdir('c:\\')
    os.system('django-admin startproject myproject')    
    os.system('django-admin startapp myapp')

def function(request):
    create_project()

If you really want to keep that part in a separate file, you could do:

In your myscript.py:

import os

def create_project:
    os.chdir('c:\\')
    os.system('django-admin startproject myproject')    
    os.system('django-admin startapp myapp')
# this would run the function when you run the script alone
if __name__ == '__main__':
    create_project()

In you views.py:

import myscript

def function(request):
    myscript.create_project()

It’s a better idea to handle python using python instead of using operating system, because os environment might change and might swallow errors that otherwise easy to figure out in python.

Leave a comment