Start a Django Project with uv

In progress Django

Goal

A new Django project managed by uv, with a first app, a first page, a Makefile target to run it and a .gitignore.

Prerequisites

  • uv installed.
  • A workspace folder for the repository (here crud-in-the-cloud/, with the Django code in django-app/).

Steps

  1. Create the uv project and add Django (this creates .venv and uv.lock):
    uv init django-app
    cd django-app
    uv add django
    
  2. Scaffold the project in the current folder (the trailing . avoids a nested folder):
    uv run django-admin startproject core .
    
  3. Create an app and register it in core/settings.py:
    uv run manage.py startapp website
    
    INSTALLED_APPS = [
        # django.contrib apps...
        "website",
    ]
    
  4. Add a view in website/views.py:
    from django.shortcuts import render
    
    def homepage(request):
        return render(request, "website/homepage.html")
    
  5. Create the template in the app's namespaced folder, website/templates/website/homepage.html (the inner website/ folder prevents name clashes between apps).
  6. Route it: website/urls.py with path("", views.homepage, name="homepage"), then include it from core/urls.py with path("", include("website.urls")).
  7. Add a Makefile target at the repository root:
    server:
    	cd django-app && uv run manage.py runserver 8000
    
  8. Add a .gitignore: .venv/, .env, __pycache__/, *.pyc, *.log, db.sqlite3, db.sqlite3-journal, media/, static_collected/, editor folders and .DS_Store.

Verify

  • make server, then http://127.0.0.1:8000/ shows the homepage.
  • git status doesn't list .venv/ or db.sqlite3.

Troubleshooting

  • TemplateDoesNotExist: check the app is in INSTALLED_APPS and the template sits in website/templates/website/.
  • Commands use the wrong Python: run them through uv run instead of a globally installed django-admin.

References