Django Cheatsheet
Django Cheatsheet
</>CodeWithHarry
Django Cheatsheet
"Django cheatsheet for beginners"
By CodeWithHarry Updated: 5 April 2025
What is Django?
Creating a project
https://www.codewithharry.com/blogpost/django-cheatsheet 1/8
5/3/25, 6:24 AM Django Cheatsheet | Blog | CodeWithHarry
Starting a server
Django MVT
Sample views.py
def index(request):
return HttpResponse("Django CodeWithHarry Cheatsheet")
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
https://www.codewithharry.com/blogpost/django-cheatsheet 2/8
5/3/25, 6:24 AM Django Cheatsheet | Blog | CodeWithHarry
Views in Django
A Python function that takes a web request and returns a web response.
def index(request):
return HttpResponse("This is a function based view.")
Django's class-based views provide an object-oriented way of organizing your view code.
class SimpleClassBasedView(View):
def get(self, request):
pass # Code to process a GET request
URLs in Django
https://www.codewithharry.com/blogpost/django-cheatsheet 3/8
5/3/25, 6:24 AM Django Cheatsheet | Blog | CodeWithHarry
urlPatterns = [
path('admin/', admin.site.urls),
path('', views.index, name='index'),
path('about/', views.about, name='about'),
]
urlpatterns = [
# ... snip ...
path('community/', include('aggregator.urls')),
path('contact/', include('contact.urls')),
# ... snip ...
]
Forms in Django
Similar to HTML forms but are created by Django using the form field.
# creating a form
class SampleForm(forms.Form):
name = forms.CharField()
description = forms.CharField()
Apps in Django
https://www.codewithharry.com/blogpost/django-cheatsheet 4/8
5/3/25, 6:24 AM Django Cheatsheet | Blog | CodeWithHarry
Creating an app
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'AppName'
]
Templates in Django
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': ["templates"],
'APP_DIRS': True,
'OPTIONS': {
# some options here
},
},
]
https://www.codewithharry.com/blogpost/django-cheatsheet 5/8
5/3/25, 6:24 AM Django Cheatsheet | Blog | CodeWithHarry
A view is associated with every URL. This view is responsible for displaying the content from
the template.
def index(request):
return render(request, 'index.html') # render is used to return the
template
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Template is working</title>
</head>
<body>
<h1>This is a sample django template.</h1>
</body>
</html>
Migrations in Django
Migrations are Django's way of updating the database schema according to the changes that
you make to your models.
Creating a migration
The below command is used to make migration (create files with information to update the
database) but no changes are made to the actual database.
https://www.codewithharry.com/blogpost/django-cheatsheet 6/8
5/3/25, 6:24 AM Django Cheatsheet | Blog | CodeWithHarry
The below command is used to apply the changes to the actual database.
Page Redirection
Redirection is used to redirect the user to a specific page of the application on the occurrence
of an event.
Redirect method
def redirecting(request):
return redirect("https://www.codewithharry.com")
Tags
Share
django cheatsheet
Main Learn
https://www.codewithharry.com/blogpost/django-cheatsheet 7/8
5/3/25, 6:24 AM Django Cheatsheet | Blog | CodeWithHarry
Home Courses
Contact Tutorials
Work With Us Notes
My Gear
Legal Social
Terms GitHub
Privacy Twitter (X)
Refund YouTube
Facebook
https://www.codewithharry.com/blogpost/django-cheatsheet 8/8