Getting started with Django: models, views, URLs and templates

Django is a batteries-included Python web framework. It ships an ORM, migrations, an admin site, authentication, forms and a template engine, so a CRUD-style site needs very few extra packages. Django calls its structure Model-View-Template (MVT). The “view” is the function that handles a request, and the “template” renders the HTML.
This walkthrough builds a page that lists blog posts. It was checked against Django 6.1, which requires Python 3.12 or later.
Install
Section titled “Install”python -m venv .venvsource .venv/bin/activate # Windows: .venv\Scripts\activatepip install djangopython -m django --versionCreate a project and an app
Section titled “Create a project and an app”A project holds settings and the root URL configuration. An app is a package with models, views and templates for one feature. A project usually contains several apps.
django-admin startproject myprojectcd myprojectpython manage.py startapp myappmyproject/ manage.py myproject/ __init__.py asgi.py settings.py urls.py wsgi.py myapp/ __init__.py admin.py apps.py migrations/ __init__.py models.py tests.py views.pyDjango doesn’t pick up the app until you add it to INSTALLED_APPS. Without that step, makemigrations doesn’t see your models and the template loader doesn’t find your templates:
INSTALLED_APPS = [ "myapp", "django.contrib.admin", # ...the rest of the defaults]Model and migrations
Section titled “Model and migrations”from django.db import models
class Post(models.Model): title = models.CharField(max_length=200) content = models.TextField() pub_date = models.DateTimeField("date published")
def __str__(self): return self.titlepython manage.py makemigrations myapppython manage.py migratemakemigrations writes a migration file describing the change. migrate applies it, along with the built-in apps’ migrations, to the database (SQLite by default).
To add posts without writing a form, register the model with the admin and create a superuser:
from django.contrib import admin
from .models import Post
admin.site.register(Post)python manage.py createsuperuserView and URLs
Section titled “View and URLs”from django.shortcuts import render
from .models import Post
def index(request): posts = Post.objects.order_by("-pub_date") return render(request, "myapp/index.html", {"posts": posts})Give the app its own URL configuration, then include it from the project’s:
from django.urls import path
from . import views
urlpatterns = [ path("", views.index, name="index"),]from django.contrib import adminfrom django.urls import include, path
urlpatterns = [ path("myapp/", include("myapp.urls")), path("admin/", admin.site.urls),]Template and static files
Section titled “Template and static files”With the default APP_DIRS: True setting, Django looks for templates in each installed app’s templates/ directory, and the static files app looks in each app’s static/ directory. Repeat the app name inside both directories (templates/myapp/, static/myapp/) so files from different apps with the same name don’t collide.
h2 { color: navy; }{% load static %}<!DOCTYPE html><html><head> <title>Posts</title> <link rel="stylesheet" href="{% static 'myapp/style.css' %}"></head><body> {% for post in posts %} <h2>{{ post.title }}</h2> <p>{{ post.content }}</p> <p>Published on {{ post.pub_date|date:"Y-m-d" }}</p> {% empty %} <p>No posts yet.</p> {% endfor %}</body></html>Run it
Section titled “Run it”python manage.py runserverAdd a post at http://127.0.0.1:8000/admin/, then open http://127.0.0.1:8000/myapp/.
Gotchas
Section titled “Gotchas”runserverserves static files only whileDEBUG = True. In production, runcollectstaticand serve the files with your web server or a package like WhiteNoise.- With
USE_TZ = True(the default), assign timezone-aware datetimes (django.utils.timezone.now()), notdatetime.now(). - Django loads templates from apps in
INSTALLED_APPSorder, and the first match wins. Without themyapp/subdirectory, another app’sindex.htmlcould shadow yours.
Further reading
Section titled “Further reading”© 2026 David Li · Home · Blog · RSS ·Classic site · Built with Astro, Starlight and three.js · Logos:Font Awesome Free (CC BY 4.0)