Start a Django Project with uv
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 indjango-app/).
Steps
- Create the uv project and add Django (this creates
.venvanduv.lock):uv init django-app cd django-app uv add django - Scaffold the project in the current folder (the trailing
.avoids a nested folder):uv run django-admin startproject core . - Create an app and register it in
core/settings.py:uv run manage.py startapp websiteINSTALLED_APPS = [ # django.contrib apps... "website", ] - Add a view in
website/views.py:from django.shortcuts import render def homepage(request): return render(request, "website/homepage.html") - Create the template in the app's namespaced folder,
website/templates/website/homepage.html(the innerwebsite/folder prevents name clashes between apps). - Route it:
website/urls.pywithpath("", views.homepage, name="homepage"), then include it fromcore/urls.pywithpath("", include("website.urls")). - Add a Makefile target at the repository root:
server: cd django-app && uv run manage.py runserver 8000 - Add a
.gitignore:.venv/,.env,__pycache__/,*.pyc,*.log,db.sqlite3,db.sqlite3-journal,media/,static_collected/, editor folders and.DS_Store.
Verify
-
make server, thenhttp://127.0.0.1:8000/shows the homepage. -
git statusdoesn't list.venv/ordb.sqlite3.
Troubleshooting
TemplateDoesNotExist: check the app is inINSTALLED_APPSand the template sits inwebsite/templates/website/.- Commands use the wrong Python: run them through
uv runinstead of a globally installeddjango-admin.
References
- Django documentation: Writing your first Django app - projects, apps, views and URLs.
- uv documentation: Working on projects -
uv init,uv add,uv run. - Django Project Setup with uv and Docker - the guide I followed.
- Django MOC - hub for Django notes.