Show Menu
Cheatography

Django Basics Cheat Sheet (DRAFT) by

Basic Django command list.

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

Create project enviro­nment

sudo apt-get install python­3-venv
Download the package
python3 -m venv env
Run the enviro­nment
.\env­\bin­\python
Select env with Ctrl+S­hift+P
python -m pip install django
Install Django

Create and run project

django­-admin startp­roject web_pr­oject
Start the project
python manage.py runserver
Run the server in 8000 port
Ctrl+C
Stop the server

Project folders and files

__init­__.py
Python info
settin­gs.py
Project settings
urls.py
Project URL config­ura­tions
wsgi.py
Enables WSGI.

Create app

#Create the app

python manage.py startapp <na­me>

#Create a view in <na­me>­/vi­ews.py

from django.http import HttpRe­sponse
def home(r­equ­est):
return HttpRe­spo­nse­("Hello, Django­!")

#Create <na­me>­/ur­ls.py and the following

from django.urls import path
from <na­me> import views
urlpat­terns = [
path("", views.h­ome, name="h­ome­"),
]

#Modify web_pr­oje­ct/­urls.py

from django.co­ntrib import admin
from django.urls import include, path

urlpat­terns = [
path("", includ­e("h­ell­o.u­rls­")),
]

App folders

views.py
App functions, views.
models.py
Contains classes defining your data objects