0

I need your help. Currently, I'm developing a Django automation script that will be useful in the future. However, I am unable to replace a specific code snippet with my updated code snippet. Updated code snippets are written at the end of the file, doubling the code snippet in the same file. You can see the code that we (me and Github Copilot) wrote together. Contributions can be made directly to the file at: https://github.com/Ketan-coder/Django_automation

import os

project_name = str(input("Whats your project name : ")).capitalize()
apps_name = str(input("What your apps name : ")).split(",")


# check if the project name is in the current directory
if os.path.isdir(project_name):
    print("Project name is already in the current directory")
    exit()
else:
    os.system('cmd /c "django-admin startproject ' + project_name)
    # change the directory to the project name
    os.chdir(project_name)
    # print the current directory
    print(os.getcwd())
    # check if the apps name is in the project directory
    if os.path.isdir(apps_name[0].capitalize()):
        print("Apps name is already in the project directory")
        exit()
    else:
        # check if apps_name has more than one app
        if len(apps_name) > 1:
            for app in apps_name:
                os.system('cmd /c "django-admin startapp ' + app.capitalize())
        else:
            os.system('cmd /c "django-admin startapp ' + apps_name[0].capitalize())
# change the directory to the project name
os.chdir('../'+project_name)
# print the current directory
print(os.getcwd())

search_text = '\nINSTALLED_APPS = [\n"django.contrib.admin",\n"django.contrib.auth",\n"django.contrib.contenttypes",\n"django.contrib.sessions",\n"django.contrib.messages",\n"django.contrib.staticfiles"]\n'
for app in apps_name:
    search_text += '"' + app.capitalize() + '.apps.' +  app.capitalize() +'Config",\n'
replace_text = search_text + ']\n'

print(replace_text)
# append the apps name to INSTALLED_APPS in settings.py
with open(project_name + '/settings.py', 'r+') as file:
    content=file.read()
    content.split(search_text)
    new_file = content[0] + replace_text + content[1]
    # content = content.replace(search_text, replace_text)
    # for app in apps_name:
    #     file.write('"' + app.capitalize() + '.apps.' +  app.capitalize() +'Config",\n')
    file.write(new_file)
    file.truncate()

Please help me with this.

Ketan
  • 23
  • 1
  • 9

0 Answers0