Show Menu
Cheatography

django basics Cheat Sheet by

django basics from django girls blog

setup

creating a project
djang­o-admin startp­roject mysite .
changing settings
edit mysite­/se­tti­ngs.py
database setup
python3 manage.py migrate
run the server
python3 manage.py runserver
and in browser:
test shit in an intera­ctive console
python3 manage.py shell

apps

creating an applic­ation
python3 manage.py startapp blog
add the app name to INSTA­LLE­D_APPS
'blog.ap­ps.B­lo­gCo­nfig'
models are kept in models.py
add, edit and delete model in admin.py

forms

import
from django import forms
 
from .models import model­_name

django querysets

getting the model classes
from app_n­ame­.m­odels import model­_name
getting the user model
from django.co­ntr­ib.a­ut­h.m­odels import User
see all objects of a certain model
Class­_na­me.o­bj­ect­s.a­ll() (is iterable)
create an object
Class­_na­me.o­bj­ect­s.c­rea­te(­att­rib­utes) you don't have to save for it to be reflected
accessing the object via attribute
Class­_na­me.o­bj­ect­s.g­et(­att­r="v­alu­e")
filtering (can return multiple, iterable)
Class­_na­me.o­bj­ect­s.f­ilt­er(­att­r="v­alu­e")
filtering (contains)
Class­_na­me.o­bj­ect­s.f­ilt­er(­tit­le_­_co­nta­ins­="ph­ras­e")
 
publi­she­d_d­ate­__l­te=­tim­ezo­ne.n­ow()
ordering
Class­_na­me.o­bj­ect­s.o­rde­r_b­y("a­ttr­ibu­te") "­-at­tr" does the reverse
you can combine ordering and filtering
Class­_na­me.o­bj­ect­s.does this first.does this second
delete
obj.de­lete()

security

login decorator
from django.co­ntr­ib.a­ut­h.d­eco­rators import login_­req­uired
 
@logi­n_r­equ­ired
 

model attributes

model attrib­ute
desc­rip­tion
args
model­s.C­har­Field
text with a limited number of characters
max_l­ength
model­s.T­ext­Field
text field with unlimited content
model­s.D­ate­Tim­eField
datetime field
default, blank, null, auto_n­ow_add

databases

creating a database
python3 manage.py migrate
update changes to database
python3 manage.py makemi­gra­tions app_n­ame
migrate
python3 manage.py migrate app_n­ame
register database in app_n­ame­/a­dmin.py
admin.si­te.r­eg­ist­er(­Mo­del­_na­me)

basic styling

get bootstrap
<link rel="st­yle­she­et" href="/­/ma­xcd­n.b­oot­str­apc­dn.c­om­/bo­ots­tra­p/3.4.1­/c­ss/­boo­tst­rap.mi­n.c­ss">
 
<link rel="st­yle­she­et" href="/­/ma­xcd­n.b­oot­str­apc­dn.c­om­/bo­ots­tra­p/3.4.1­/c­ss/­boo­tst­rap­-th­eme.mi­n.c­ss">
add static files
make a static folder with css files inside app_name folder
 
{% load static %}
 
<link rel="st­yle­she­et" href="{% static 'css/b­log.css' %}">
create a block
{% block content %}
 
{% endblock %}
block content
{% block content %}
 
{% endblock %}
connect a block to the base
{% extends 'blog/­bas­e.html' %}
link a url within a site
a href="{% url 'url_­name' pk=post.pk %}"
 
where pk stands for primary key
to use the link
path(­'w­hat­eve­r/­<in­t:p­k>/', views.­vi­ew_­name, name='­*ur­l_n­ame')

admin

register a model
admin.si­te.r­eg­ist­er(­Post)
create superuser
python3 manage.py create­sup­eruser

python­any­where deployment

1. create the api token
2. new bash console
3. in bash: pip3 install --user python­any­where
4. pa_au­toc­onf­igu­re_­dja­ngo.py --pyth­on=3.8 repo_­lin­k.git
5. to update website, go to your directory and pull code
6. reload at web
7. workon your_­dir­ect­ory
8. python3 manage.py collec­tst­atic

Help Us Go Positive!

We offset our carbon usage with Ecologi. Click the link below to help us!

We offset our carbon footprint via Ecologi
 

Comments

No comments yet. Add yours below!

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.

          Related Cheat Sheets

          Python Cheat Sheet
          Django Basics Cheat Sheet