0% found this document useful (0 votes)
75 views

Django Cheatsheet

Uploaded by

TheRitik
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
75 views

Django Cheatsheet

Uploaded by

TheRitik
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

DJANGO CHEAT SHEET DJANGO CHEAT SHEET

What is Django? Sample Class-Based Views


Python-based web framework used for rapid development. Django's class-based views provide an object-oriented (OO) way of organizing your view code.
from django.views import View
Installing Django + Setup class SimpleClassBasedView(View): def
pip install django get(self, request):
Creating a project ... # code to process a GET request
The below command creates a new project URLs in Django
django-admin startproject projectName set of URL patterns to be matched against the requested URL.
Starting a server Sample urls.py file
The below command starts the development server. from django.contrib import admin
python manage.py runserver from django.urls import path from
Django MVT . import views
urlpatterns = [ path('admin/',
Django follows MVT(Model, View, Template) architecture.
admin.site.urls), path('', views.index,
Sample Django Model
The model represents the schema of the database. name='index'), path('about/', views.about,
from django.db import models name='about'), ]
class Product(models.Model): //Product is the name of our model
product_id=models.AutoField Forms in Django
Sample views.py Similar to HTML forms but are created by Django using the form field.
View decides what data gets delivered to the template. Sample Django form
from django.http import HttpResponse from django import forms
def index(request): # creating a form class
return HttpResponse(''Django CodeWithHarry Cheatsheet'') SampleForm(forms.Form): Name =
Sample HTML Template forms.CharField() description =
A sample .html file that contains HTML, CSS and Javascript. forms.CharField()
<!DOCTYPE html> Apps in Django
<html lang="en">
<head> Apps in Django are like independent modules for different functionalities.
<meta charset="UTF-8"> Creating an app
<meta name="viewport" content="width=device-width, initial-scale=1.0"> python manage.py startapp AppName
<meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>CodeWithHarry Listing app in the settings.py
Cheatsheet</title>
After creating an app, we need to list the app name in INSTALLED_APPS INSTALLED_APPS = [
</head>
'django.contrib.admin',
<body>
'django.contrib.auth',
<h1>This is a sample template file.</h1>
'django.contrib.contenttypes',
</body>
'django.contrib.sessions',
</html> 'django.contrib.messages', 'django.contrib.staticfiles',
Views in Django 'AppName'
]
Sample Function-Based Views
A python function that takes a web request and returns a web response. Templates in Django
from django.http import HttpResponse Used to handle dynamic HTML files separately.
def index(request): return HttpResponse(''This is a Configuring templates in settings.py
function based view.') TEMPLATES = [
{

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

You might also like