Show Menu

Django Cheat Sheet (DRAFT) by

Django cheet sheet for learning django

This is a draft cheat sheet. It is a work in progress and is not finished yet.


What it does
user = models.Fo­rei­gnK­ey(­User, on_del­ete­=mo­del­s.C­ASCADE, null=True, blank=­True)
User the default django user (import it firstly)
title = models.Ch­arF­iel­d(m­ax_­len­gth­=200)
Char Field
descri­ption = models.Te­xtF­iel­d(n­ull­=True, blank=­True)
Text Field
complete = models.Bo­ole­anF­iel­d(d­efa­ult­=False)
Boolean Field
created = models.Da­teT­ime­Fie­ld(­aut­o_n­ow_­add­=True)
Date Time Field
def __str_­_(s­elf): return self.title
The string repres­ent­ation of the model
class Meta: ordering = ['comp­lete']
Ordering the data

File Edits

The file
The change
What it does
To add the new app to django settings (of the app)
from django.urls import path
To import the path (of the app)
from . import views
To import the view file (of the app)
urlpat­terns = [ ]
To add the urls you need (of the project)
from django.urls import path, include
Import include (of the project)
path('', includ­e('­App­_Na­me.u­rl­s')),
To add the applic­ation urls
from­ntr­ib.a­ut­h.m­odels import User
To user the default user tabel from django
from .models import Model_Name
To import the new created model
To add the new created model to the admin panel


What it does
python -m venv env
Create virtual enviro­nment
Activate python virtual enviro­nment
pip install django
To install django
django­-admin startp­roject Projec­t_Name
To create the project
python startapp APP_Name
To create an app inside the project
python runserver
To run the server
python makemi­gra­tions
Changes in the DB
python migrate
Send the changes to the DB
python create­sup­eruser
Make an admin user