Show Menu
Cheatography

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.

Models

Code
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
settin­gs.py
APP_Na­me.a­pp­s.A­PP_­Nam­eConfig
To add the new app to django settings
urls.py (of the app)
from django.urls import path
To import the path
urls.py (of the app)
from . import views
To import the view file
urls.py (of the app)
urlpat­terns = [ ]
To add the urls you need
urls.py (of the project)
from django.urls import path, include
Import include
urls.py (of the project)
path('', includ­e('­App­_Na­me.u­rl­s')),
To add the applic­ation urls
models.py
from django.co­ntr­ib.a­ut­h.m­odels import User
To user the default user tabel from django
admin.py
from .models import Model_Name
To import the new created model
admin.py
admin.s­it­e.r­egi­ste­r(M­ode­l_Name)
To add the new created model to the admin panel

Commands

Command
What it does
python -m venv env
Create virtual enviro­nment
"./e­nv/­Scr­ipt­s/a­cti­vat­e"
Activate python virtual enviro­nment
pip install django
To install django
django­-admin startp­roject Projec­t_Name
To create the project
python manage.py startapp APP_Name
To create an app inside the project
python manage.py runserver
To run the server
python manage.py makemi­gra­tions
Changes in the DB
python manage.py migrate
Send the changes to the DB
python manage.py create­sup­eruser
Make an admin user