Django (web framework)/Cheat sheet
Last edited by dave on 14/02/2026, 10:07:11 UTC
Django (web framework) / Cheat sheet
Contents
The cheat sheet of Django.
Django 4.x / 5.x quick reference for projects, ORM, views, templates, auth, and production basics.
0. Quick Start
Install:
pip install django
Create project:
django-admin startproject mysite cd mysite python manage.py runserver
Create app:
python manage.py startapp blog
Add app to INSTALLED_APPS.
1. Project Structure
mysite/
├─ manage.py
├─ mysite/
│ ├─ settings.py
│ ├─ urls.py
│ ├─ asgi.py
│ └─ wsgi.py
├─ blog/
2. URL Routing
Project urls.py:
from django.urls import path, include urlpatterns = [ path("blog/", include("blog.urls")), ]
App urls.py:
from django.urls import path from . import views urlpatterns = [ path("", views.index), ]
3. Views
Function view:
from django.http import HttpResponse def index(request): return HttpResponse("hello")
Template view:
from django.shortcuts import render def home(request): return render(request, "home.html")
4. Templates
Example:
<h1>Hello {{ name }}</h1>
Loop:
{% for item in items %} {{ item }} {% endfor %}
Condition:
{% if user.is_authenticated %}
5. Models (ORM)
Example:
from django.db import models class Post(models.Model): title = models.CharField(max_length=200) body = models.TextField()
6. Migrations
python manage.py makemigrations python manage.py migrate
7. ORM Queries
Create:
Post.objects.create(title="Hello")
Get:
Post.objects.get(id=1)
Filter:
Post.objects.filter(title="Hello")
Delete:
post.delete()
8. Admin
Register model:
from django.contrib import admin from .models import Post admin.site.register(Post)
Create admin user:
python manage.py createsuperuser
9. Forms
from django import forms class PostForm(forms.Form): title = forms.CharField()
ModelForm:
from django.forms import ModelForm class PostForm(ModelForm): class Meta: model = Post fields = ["title", "body"]
10. Static Files
Settings:
STATIC_URL = "/static/"
Template:
{% load static %} <script src="{% static 'app.js' %}"></script>
11. Authentication
Login required:
from django.contrib.auth.decorators import login_required @login_required def dashboard(request): pass
12. Class-Based Views
from django.views.generic import ListView from .models import Post class PostListView(ListView): model = Post
13. Management Commands
Example:
from django.core.management.base import BaseCommand class Command(BaseCommand): def handle(self, *args, **kwargs): print("done")
Run:
python manage.py mycommand
14. Production Basics
Never in production:
DEBUG=True
Typical stack:
- PostgreSQL
- Gunicorn
- Nginx
collectstatic- environment variables
Collect static:
python manage.py collectstatic
Run gunicorn:
gunicorn mysite.wsgi
Backlinks (4)
- General
- User
No backlinks yet.
- Redirects
No backlinks yet.
- Media
No backlinks yet.
- Categories
No backlinks yet.
Categories (0)
No categories assigned to this page.
Edit Level
> Signed In Users
Latest on Lounge
Join the conversation about the 'Django (web framework)/Cheat sheet' article →
No comments yet. Be the first to comment!