django cheat sheet

made for perfectionists with deadlines.
Star
Share

start a project

django-admin startproject project_name

start the server

cd into your project's root directory*
to start Django server

python manage.py runserver

the root is where your manage.py lives

create an app

an app is a directory containing code functionality able to live by itself apart from the other parts of the website

python manage.py startapp app_name

workflow

When Django finds a regular expression match, Django calls the
specified view function, with an HttpRequest object as the first argument
and any “captured” values from the regular expression as other arguments.

set URLconf

create a file called urls.py in your app directory and set urlpatterns
for example set / to point to index.html:
urlpatterns = [ url(r'^$', views.index, name='index'), ]

add urls.py in your app's directory and register to let it route its internal links:

url(r'^app_name/', include('app_name.urls'))

include the app

add a reference to its configuration class in the INSTALLED_APPS setting

'app_name.apps.AppnameConfig'

migrate

running makemigrations tells Django that you’ve made some
changes to your models and that you’d like
the changes to be stored as a migration.

python manage.py makemigrations polls

apply the migration

python manage.py migrate

bonus: get the SQL of a migration
read the SQL command to be execute before you apply the migration

python manage.py sqlmigrate app_name 0001

Django's API

play with the django admin console:

python manage.py shell

Creating an admin user

python manage.py createsuperuser

and login at http://127.0.0.1:8000/login/

make an app modifiable in the admin

In app_name/admin.py, import the model

from .models import Modelname

register the model to the Admin

admin.site.register(Modelname)

links & resources


made with by Bar & Liz Horing Amir
inspired by Roger Dudler's git - the simple guide

please report issues on github