Show Menu
Cheatography

django basics Cheat Sheet by

django basics from django girls blog

setup

creating a project
django­-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
INSTAL­LED­_APPS
'blog.a­pp­s.B­log­Config'
models are kept in models.py
add, edit and delete model in admin.py

django querysets

getting the model classes
from app_name.models 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_­nam­e.o­bje­cts.all()
(is iterable)
create an object
Class_­nam­e.o­bje­cts.cr­eat­e(a­ttr­ibutes)
you don't have to save for it to be reflected
accessing the object via attribute
Class_­nam­e.o­bje­cts.ge­t(a­ttr­="va­lue­")
filtering (can return multiple, iterable)
Class_­nam­e.o­bje­cts.fi­lte­r(a­ttr­="va­lue­")
filtering (contains)
Class_­nam­e.o­bje­cts.fi­lte­r(t­itl­e__­con­tai­ns=­"­phr­ase­")
 
publis­hed­_da­te_­_lt­e=t­ime­zon­e.now()
ordering
Class_­nam­e.o­bje­cts.or­der­_by­("at­tri­but­e")
"­-at­tr" does the reverse
you can combine ordering and filtering
Class_­nam­e.o­bje­cts.does this first.does this second
delete
obj.delete()

forms

import
from django import forms
 
from .models import model_name

security

login decorator
from django.co­ntr­ib.a­ut­h.d­eco­rators import login_­req­uired
 
@login­_re­quired
 

model attributes

model attribute
descri­ption
args
models.Ch­arField
text with a limited number of characters
max_length
models.Te­xtField
text field with unlimited content
models.Da­teT­ime­Field
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_name
migrate
python3 manage.py migrate app_name
register database in app_name/admin.py
admin.s­it­e.r­egi­ster(Model_name)

admin

register a model
admin.s­it­e.r­egi­ste­r(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_aut­oco­nfi­gur­e_d­jan­go.py --pyth­on=3.8 repo_l­ink.git
5. to update website, go to your directory and pull code
6. reload at web
7.
workon your_d­ire­ctory
8.
python3 manage.py collec­tstatic

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('whatever/<i­nt:­pk>/', views.view_name, name='­*ur­l_n­ame')
 

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