Creating a Basic Django Project Automatically

Required: have an image logo.png in the same directory of the script below.


Result:
mysite/
mysite/
settings.py
urls.py
polls/
templates/
polls/
index.html
views.py
urls.py
static/
image/
logo.png
manage.py
 
logo.png

import subprocess
import os 
import shutil

project_name = "mysite"
app_name = "polls"



# Creating project and app
subprocess.run('django-admin startproject '+project_name)
os.chdir(project_name)
subprocess.run('python manage.py startapp '+app_name)



# Modifying settings.py
os.chdir(project_name)
with open('settings.py', 'r') as original, open('temp.py', 'w') as modified:
    for line in original:
        modified.write(line)

        if 'INSTALLED_APPS' in line:
            modified.write('\t"'+app_name + '",\n')


    modified.write('STATICFILES_DIRS = [ BASE_DIR / "static" ]\n')
    modified.write("MEDIA_ROOT = BASE_DIR / 'media'\n")
    modified.write("MEDIA_URL = '/media/'\n")

os.replace('temp.py', 'settings.py')




# views.py
os.chdir('..')
os.chdir(app_name)

code = f"""
from django.views.generic import TemplateView

class index(TemplateView):
    template_name = '{app_name}/index.html'
"""

with open('views.py', 'w') as file:
    file.write(code)



# urls.py (app)
code = """
from django.urls import path
from .views import *

urlpatterns = [
    path("", index.as_view(), name="index"),
]
"""
with open('urls.py', "w") as file:
    file.write(code)




# urls.py (project)
os.chdir('..')
os.chdir(project_name)

code = f"""
from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static

urlpatterns = [
    path("admin/", admin.site.urls),
    path("", include("{app_name}.urls")),
]

urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
"""

with open('urls.py', 'w') as file:
    file.write(code)




# static + templates
os.chdir('..')
os.mkdir('static')
os.chdir('static')
os.mkdir('images')
os.chdir('..')
os.chdir(app_name)
os.mkdir('templates')
os.chdir('templates')
os.mkdir(app_name)


# html 
os.chdir(app_name)
code = """
{% load static %}
<!DOCTYPE html>
<html lang="en" dir="ltr">
    <head>
        <meta charset="utf-8">
    </head>
    <body style="background-color:#303030; color:#e0e0e0;">

        <h1>Hello World!</h1>
        <img src="{% static 'images/logo.png' %}" alt="">
    </body>
</html>
"""
with open('index.html', "w") as file:
    file.write(code)





os.chdir('..')
os.chdir('..')
os.chdir('..')
os.chdir('..')


shutil.copy('logo.png', project_name+'/static/images/')


os.chdir(project_name)

subprocess.run('python manage.py migrate')
subprocess.run('python manage.py createsuperuser')
subprocess.run('python manage.py runserver')





 

python django.py

 

 

Tested with:
Django==5.1


07 May 2025 | Last Updated: 21 Nov. 2025 | jaimedcsilva

Related
  • Using ngrok with Django
  • Opening a Django project through a .exe file
  • Creating an online store with Django
  • CRUD
  • Creating a Basic Django Project Automatically
  • Filter Horizontal in a custom template
  • GeoIP tracking with IpInfo and Django
  • GeoIP tracking with MaxMind and Django
  • Django User Agents
  • Generating Temporary Download Links
  • Cython - Hiding the Code of a Django Project
  • Quick & Easy Django Deployment on PythonAnywhere
  • A Brief History of Django
  • Django & Paypal Webhooks
  • Generating QR Codes with Django

  • Buy Me a Coffee