Django Cheat Sheet

The Django Cheat Sheet is a quick reference list of many of the most commonly used commands and techniques for Django.

What is Django?

Django is a python-based web framework used for the rapid development of web applications.

Django Cheat Sheet

Installing Django

pip install django

Create a project

django-admin startproject projectName

Start a server

python manage.py runserver

Django Model

from django.db import models

class Book(models.Model):
    book_id=models.AutoField

Django views.py

from django.http import HttpResponse

def index(request):
    return HttpResponse("Django CodeWithBeri Cheatsheet")

HTML Template

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>CodeWithBeri Cheatsheet</title>
</head>
<body>
    <h1>This is a sample template file.</h1>
</body>
</html>

Function-Based Views

from django.http import HttpResponse

def index(request):
    return HttpResponse("This is a function based view.")

Class-Based Views

from django.views import View

class SimpleClassBasedView(View):
    def get(self, request):
        pass # Code to process a GET request

Django urls.py

from django.contrib import admin
from django.urls import path, include
from . import views

urlPatterns = [
    path('admin/', admin.site.urls),
    path('', views.index, name='index'),
    path('about/', views.about, name='about'),
    path('contact/', include('contact.urls')),
]

Django form

from django import forms

# creating a form
class DjangoForm(forms.Form):
    name = forms.CharField()
    description = forms.CharField()

Create an app

python manage.py startapp app_name

Register app in the settings.py

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'app_name'
]

Configuring templates in settings.py

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': ["templates"],
        'APP_DIRS': True,
        'OPTIONS': {
            # some options here
        },
    },
]

Create a migration file

python manage.py makemigrations

Applying the migration

python manage.py migrate

Creating the admin user

python manage.py createsuperuser

Redirect method

from django.shortcuts import render, redirect

def redirecting(request):
    return redirect("https://www.codewithberi.xyz")

Note: If you are a beginner in python web development then I will suggest you learn flask first. For helping you, I also create a flask cheat sheet.

Conclusion

The Django framework is an open-source web framework that encourages rapid development and clean design. It is one of the most popular frameworks for the Python programming language.

The Django framework has a lot of features like user authentication, administration, and templates. The Django cheat sheet is a great resource for beginners who want to learn about the framework.

Leave a Comment