Django Cheat Sheet
Quick reference for Django framework. Commands, models, views, URLs, templates, forms, and ORM queries — all in one page.
Management Commands
| django-admin startproject myproject | Create a new Django project |
| python manage.py startapp myapp | Create a new app within project |
| python manage.py runserver | Start development server on port 8000 |
| python manage.py makemigrations | Generate migration files from model changes |
| python manage.py migrate | Apply pending database migrations |
| python manage.py createsuperuser | Create admin superuser account |
| python manage.py collectstatic | Collect static files into STATIC_ROOT |
| python manage.py shell | Open interactive Django shell |
| python manage.py test | Run all tests |
| python manage.py dbshell | Open database command-line client |
Models & Fields
| class Post(models.Model): | Define a model (maps to DB table) |
| title = models.CharField(max_length=200) | Short text field with max length |
| body = models.TextField() | Long text field (no max length) |
| count = models.IntegerField(default=0) | Integer field with default value |
| price = models.DecimalField(max_digits=10, decimal_places=2) | Precise decimal field |
| is_active = models.BooleanField(default=True) | Boolean field |
| created = models.DateTimeField(auto_now_add=True) | Auto-set on creation |
| updated = models.DateTimeField(auto_now=True) | Auto-set on every save |
| author = models.ForeignKey(User, on_delete=models.CASCADE) | Foreign key relationship |
| tags = models.ManyToManyField(Tag) | Many-to-many relationship |
ORM Queries
| Post.objects.all() | Get all records |
| Post.objects.get(id=1) | Get single record (raises DoesNotExist) |
| Post.objects.filter(title__contains="django") | Filter with field lookup |
| Post.objects.exclude(is_active=False) | Exclude matching records |
| Post.objects.order_by("-created") | Order by field (- for descending) |
| Post.objects.filter(created__year=2026) | Filter by date component |
| Post.objects.values("title", "author__name") | Select specific fields as dicts |
| Post.objects.annotate(total=Count("comments")) | Add aggregate annotation |
| Post.objects.aggregate(Avg("price")) | Compute aggregate over queryset |
| Post.objects.select_related("author") | Join and cache FK in single query |
URL Patterns
| path("", views.home, name="home") | Route root URL to view |
| path("posts/", views.post_list, name="post-list") | Route to function-based view |
| path("posts/<int:pk>/", views.post_detail, name="post-detail") | URL with integer parameter |
| path("posts/<slug:slug>/", views.post_detail, name="post-detail") | URL with slug parameter |
| path("api/", include("api.urls")) | Include URLs from another app |
| re_path(r"^archive/(?P<year>[0-9]{4})/$", views.archive) | Regex URL pattern |
Views & Templates
| def home(request): return render(request, "home.html", context) | Function-based view with template |
| def api(request): return JsonResponse({"key": "value"}) | Return JSON response |
| class PostList(ListView): model = Post | Class-based list view |
| class PostDetail(DetailView): model = Post | Class-based detail view |
| class PostCreate(CreateView): model = Post; fields = ["title", "body"] | Class-based create view |
| {{ variable }} | Template variable interpolation |
| {% for item in items %}...{% endfor %} | Template for loop |
| {% if condition %}...{% elif %}...{% else %}...{% endif %} | Template conditional |
| {% extends "base.html" %} | Template inheritance |
| {% block content %}...{% endblock %} | Define/override template block |
Forms
| class PostForm(forms.ModelForm): class Meta: model = Post; fields = ["title", "body"] | ModelForm from model |
| class ContactForm(forms.Form): email = forms.EmailField() | Standalone form class |
| form = PostForm(request.POST) | Bind form to POST data |
| if form.is_valid(): form.save() | Validate and save form |
| form.cleaned_data["title"] | Access validated field data |
| {{ form.as_p }} | Render form as paragraphs in template |
| {% csrf_token %} | CSRF protection token (required in forms) |
| form.errors | Access validation errors dict |
Step-by-Step Guide
Read Guide →