Django Cheatsheet
Django Cheatsheet
1/3 2/3
DJANGO CHEAT SHEET
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': ["templates"],
'APP_DIRS': True,
'OPTIONS': { # ... some
options here ...
},
},
]
Changing the views.py file
A view is associated with every template file. 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 templat
Sample template file
<!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 but no changes are made to the actual database.
python manage.py makemigrations
Applying the migration
The below command is used to apply the changes to the actual database.
python manage.py migrate
Admin interface in Django
Django comes with a ready-to-use admin interface.
Creating the admin user
python manage.py createsuperuser
Page Redirection
Redirection is used to redirect the user to a specific page of the application on the occurrence of
an event.
Redirect method
from django.shortcuts import render, redirect
def redirecting(request):
return redirect("https://www.codewithharry.com")
3/3