🎓 A1Learner
🔍
0 of 50 lessons completed

Django Project Layout

A project layout is the file map of a project. Each file has one job, so every part is easy to find.

The layout

This tree shows the files of a small project. Each file has one job.

manage.py
mysite/
    settings.py
    urls.py
blog/
    views.py
    models.py
templates/
    home.html
static/
    style.css

Build the layout

This code makes the folders and writes the seven files. Each file starts with one line.

import os

os.makedirs("mysite", exist_ok=True)
os.makedirs("blog", exist_ok=True)
os.makedirs("templates", exist_ok=True)
os.makedirs("static", exist_ok=True)
with open("manage.py", "w") as f:
    f.write("import django\n")
with open("mysite/settings.py", "w") as f:
    f.write("DEBUG = True\n")
with open("mysite/urls.py", "w") as f:
    f.write("from django.urls import path\n")
with open("blog/views.py", "w") as f:
    f.write("from django.http import HttpResponse\n")
with open("blog/models.py", "w") as f:
    f.write("from django.db import models\n")
with open("templates/home.html", "w") as f:
    f.write("<h1>Hi</h1>")
with open("static/style.css", "w") as f:
    f.write("h1 { color: blue; }")

Check the files exist

This loop checks each file in turn. It prints the name with True for a file that is there.

for path in ["manage.py", "mysite/settings.py", "mysite/urls.py", "blog/views.py", "blog/models.py", "templates/home.html", "static/style.css"]:
    print(path, os.path.exists(path))

Example

import os

os.makedirs("mysite", exist_ok=True)
os.makedirs("blog", exist_ok=True)
os.makedirs("templates", exist_ok=True)
os.makedirs("static", exist_ok=True)
with open("manage.py", "w") as f:
    f.write("import django\n")
with open("mysite/settings.py", "w") as f:
    f.write("DEBUG = True\n")
with open("mysite/urls.py", "w") as f:
    f.write("from django.urls import path\n")
with open("blog/views.py", "w") as f:
    f.write("from django.http import HttpResponse\n")
with open("blog/models.py", "w") as f:
    f.write("from django.db import models\n")
with open("templates/home.html", "w") as f:
    f.write("<h1>Hi</h1>")
with open("static/style.css", "w") as f:
    f.write("h1 { color: blue; }")

for path in ["manage.py", "mysite/settings.py", "mysite/urls.py", "blog/views.py", "blog/models.py", "templates/home.html", "static/style.css"]:
    print(path, os.path.exists(path))

Key points

  • manage.py runs the project commands.
  • mysite/settings.py holds the project settings.
  • mysite/urls.py holds the address list.
  • blog/ holds one app, with its own views and models.
  • templates/ holds the template files.
  • static/ holds CSS and images.

In this example, the program makes the folders and writes seven files for a small project. The loop then looks for each file in turn. The prints pair each name with True, so all seven files are there.

Note

A folder per job keeps the project tidy.

Try it Yourself

Edit the code, then press Run.

Running is simulated

Exercise

Which file holds the project settings in the small project layout?