320 x 50 Mobile Ad
Cheat Sheets

Django Cheat Sheet

Transform, format, and optimize your data instantly with this free client-side tool.

Management

django-admin startproject <name>

Create a new Django project

python manage.py startapp <app_name>

Create a new Django app

python manage.py runserver

Start the development server

python manage.py createsuperuser

Create an admin user

python manage.py shell

Open the interactive Python shell

python manage.py collectstatic

Collect static files into STATIC_ROOT

Migrations

python manage.py makemigrations

Create new migrations based on model changes

python manage.py migrate

Apply migrations to the database

python manage.py showmigrations

List all migrations and their status

python manage.py sqlmigrate <app> <mig>

Print the SQL for the named migration

Models

models.CharField(max_length=255)

String field for short to mid-length text

models.TextField()

Large text field

models.IntegerField()

Integer field

models.BooleanField(default=False)

True/False boolean field

models.DateTimeField(auto_now_add=True)

Timestamp set when object is created

models.ForeignKey(OtherModel, on_delete=models.CASCADE)

Many-to-one relationship

models.ManyToManyField(OtherModel)

Many-to-many relationship

models.OneToOneField(OtherModel, on_delete=models.CASCADE)

One-to-one relationship

def __str__(self): return self.name

Set object string representation in admin and shell

ORM Querying

Model.objects.all()

Get all records

Model.objects.get(id=1)

Get a single record (throws DoesNotExist if missing)

Model.objects.filter(status='active')

Filter records

Model.objects.exclude(status='inactive')

Exclude records matching condition

Model.objects.order_by('-created_at')

Order by field (descending)

Model.objects.create(name='test')

Create and immediately save a new record

Model.objects.filter(name__icontains='test')

Case-insensitive substring match

Model.objects.filter(created_at__gte=past_date)

Greater than or equal to comparison

Model.objects.select_related('foreign_key')

Eager load related objects (Performance)

Model.objects.count()

Count number of records

Views & URLs

from django.http import HttpResponse def my_view(request): return HttpResponse('OK')

Simple Function-Based View (FBV)

from django.shortcuts import render def my_view(request): return render(request, 'tmplt.html', context)

Render a template

from django.shortcuts import redirect def my_view(request): return redirect('url-name')

Redirect to another URL

from django.urls import path urlpatterns = [ path('home/', views.home, name='home'), ]

Define a URL pattern

path('article/<int:id>/', views.detail)

URL with an integer parameter

Templates

{{ variable }}

Output a variable in a template

{% if condition %}...{% endif %}

If-else conditions

{% for item in list %}...{% endfor %}

For loop iteration

{% extends 'base.html' %}

Inherit a parent template

{% block content %}...{% endblock %}

Define or override a template block

{% include 'partial.html' %}

Include another template

{% url 'home' %}

Reverse resolve a URL by name

{% static 'css/style.css' %}

Generate a URL for a static file

{% csrf_token %}

CSRF protection for POST forms

{{ obj.created_at|date:'Y-m-d' }}

Apply a filter to format a date

Advanced

Advanced Command Snippet

Explore advanced configurations for Django

System Optimization

Performance tuning best practices for Django

Security Audit

Run security checks and validation for Django

320 x 50 Mobile Ad

Frequently Asked Questions

Got questions? We've got answers.

The Django Cheatsheet is a quick-reference guide that provides developers with the most essential and frequently used Django commands, syntax, and snippets in one centralized place.
This reference is built for both beginners who are just learning Django and need a quick syntax lookup, as well as seasoned professionals who need to jog their memory on complex commands.
You can use the real-time search bar at the top of the cheatsheet. Simply type a keyword (like &#39;delete&#39; or &#39;file&#39;) and the list will instantly filter to show only matching Django commands.
Yes! Every command block features a one-click copy button. Just hover over the command and click the copy icon to instantly send the snippet to your clipboard.
The cheatsheet is divided into logical categories such as Basics, Network, Operations, and specific Django features. You can click on the category filters at the top to isolate specific groups of commands.
Absolutely. This Django reference guide is 100% free, requiring no sign-ups or subscriptions, and is always available when you need it.
While these are standard Django operations, you should always understand what a command does before running it, especially if it involves system operations or destructive actions.
Yes, we have recently expanded this cheatsheet to include advanced snippets, best practices, and edge-case syntax that go beyond basic introductory commands.
ADVERTISEMENT
Boost Your Business Online