Part 3: Django template

The coding agent should not start from an empty directory. Lovable and similar tools begin from prepared templates, then ask the model to modify those templates. We use the same idea with Django.

Template first

A working template improves the odds that the generated result runs. The dependencies are already installed, Django already knows about the app, the base template exists, and the file structure is predictable.

The fastest path is the prepared repository, so we do not spend the whole workshop typing Django boilerplate:

git clone https://github.com/alexeygrigorev/django_template.git

Install dependencies and run the migrations:

cd django_template
uv sync
make migrate

Start the server:

make run

Open http://127.0.0.1:8000/. You should see the basic Django page from the template. Stop the server before continuing because the agent tool is not allowed to run runserver from inside Jupyter.

Build the template from scratch

The template README also includes the from-scratch path. You do not need to run this if you cloned the template, but it explains what is inside the starter project.

Create a new project folder:

mkdir django_template
cd django_template
uv init
rm main.py

Add Django:

uv add django

Create the Django project and app:

uv run django-admin startproject myproject .
uv run python manage.py startapp myapp

Register myapp in myproject/settings.py:

INSTALLED_APPS = [
    "django.contrib.admin",
    "django.contrib.auth",
    "django.contrib.contenttypes",
    "django.contrib.sessions",
    "django.contrib.messages",
    "django.contrib.staticfiles",
    "myapp",
]

The agent modifies this project later. Registering the app now means the generated models, views, and templates have somewhere to go.

Makefile

Add a small Makefile so the human and the agent can run common commands without remembering the full uv run invocation:

.PHONY: install migrate run test clean

install:
    uv sync --dev

migrate:
    uv run python manage.py migrate

run:
    uv run python manage.py runserver

Add test and clean commands if you want the same shape as the reference template:

test:
    uv run python manage.py test

clean:
    rm -rf __pycache__ myapp/__pycache__ myproject/__pycache__
    rm -rf db.sqlite3

make run is for you, not for the agent. The file tool we write later blocks runserver, because a long-running server process hangs the notebook execution.

Shared base template

Create templates/base.html:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>{% block title %}My Site{% endblock %}</title>
    <script src="https://cdn.tailwindcss.com"></script>
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.1/css/all.min.css" rel="stylesheet">
    {% block extra_head %}{% endblock %}
</head>
<body class="min-h-screen flex flex-col">
    {% block content %}{% endblock %}
</body>
</html>

Tell Django to look in the project-level templates directory by setting DIRS in myproject/settings.py:

TEMPLATES = [
    {
        "BACKEND": "django.template.backends.django.DjangoTemplates",
        "DIRS": [BASE_DIR / "templates"],
        "APP_DIRS": True,
        "OPTIONS": {
            "context_processors": [
                "django.template.context_processors.request",
                "django.contrib.auth.context_processors.auth",
                "django.contrib.messages.context_processors.messages",
            ],
        },
    },
]

The workshop prompt later tells the agent to keep Tailwind in the base template so generated apps do not look broken when the model accidentally removes the CSS import.

Home view

Create a simple home view in myapp/views.py:

from django.shortcuts import render

def home(request):
    return render(request, "home.html")

Connect it in myproject/urls.py:

from django.contrib import admin
from django.urls import path
from myapp import views

urlpatterns = [
    path("admin/", admin.site.urls),
    path("", views.home, name="home"),
]

Create myapp/templates/home.html:

{% extends 'base.html' %}

{% block content %}
<h1>Home</h1>
{% endblock %}

Now we have the thing a coding agent needs most: a small, working app with clear conventions. Next we create the file tools that let the model read and edit a copy of this template.

Continue with Part 4: File tools.

Questions & Answers (0)

Sign in to ask questions