Django Cheat Sheet
Commands
django-admin startproject mysite .
python manage.py startapp notes
python manage.py runserver
python manage.py makemigrations && python manage.py migrate
python manage.py createsuperuser
python manage.py shell
python manage.py collectstatic
python manage.py test
Models and queries
title = models.CharField(max_length=120)
author = models.ForeignKey(User, on_delete=models.CASCADE)
Note.objects.filter(title__icontains="x").order_by("-created_at")[:10]
Note.objects.get(pk=1)
Note.objects.select_related("author").prefetch_related("tags")
Category.objects.annotate(n=Count("notes"))
Views and URLs
path("<int:pk>/", views.detail, name="detail")
include("notes.urls")
get_object_or_404(Note, pk=pk)
render(request, "t.html", {"x": 1})
redirect("notes:list")
JsonResponse({"ok": True})
Templates
// inheritance, loops, urls, filters
{% extends "base.html" %} {% block content %}{% endblock %}
{% for n in notes %}{{ n.title|upper }}{% empty %}None{% endfor %}
{% url 'notes:detail' n.pk %} {% load static %} {% static 'a.css' %}
{% csrf_token %}
Full Django documentation