Show Menu
Cheatography

Writing your first Django app Cheat Sheet (DRAFT) by

https://docs.djangoproject.com/en/2.0/intro/tutorial01/

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

part 1

python -m django --version
You can tell Django is installed and which version
django­-admin startp­roject mysite
Creating a project
python manage.py startapp polls
Creating the Polls app
url() argument: regex
Note that these regular expres­sions do not search GET and POST parame­ters, or the domain name. For example, in a request to https:­//w­ww.e­xa­mpl­e.c­om/­myapp/, the URLconf will look for myapp/. In a request to https:­//w­ww.e­xa­mpl­e.c­om/­mya­pp/­?pa­ge=3, the URLconf will also look for myapp/.

part 2

python manage.py migrate
looks at the INSTAL­LED­_APPS setting and creates any necessary database tables according to the database settings in your mysite­/se­tti­ngs.py file and the database migrations shipped with the app (we’ll cover those later). You’ll see a message for each migration it applies.
python manage.py makemi­gra­tions polls
you’re telling Django that you’ve made some changes to your models (in this case, you’ve made new ones) and that you’d like the changes to be stored as a migration.
python manage.py sqlmigrate polls 0001
The sqlmigrate command takes migration names and returns their SQL:
python manage.py check
this checks for any problems in your project without making migrations or touching the database.

part 3

?P<­pk>
is a Python extension to the standard regex syntax. It means assign the results of this group (bounded by parens) to the variable name inside. In this case, assign the regex result to pk.htt­ps:­//t­eam­tre­eho­use.co­m/c­omm­uni­ty/­hel­p-m­e-u­nde­rst­and-ppk
The template system
The template system uses dot-lookup syntax to access variable attrib­utes.

part 4

forloo­p.c­ounter
indicates how many times the for tag has gone through its loop
except (KeyError, Choice.Do­esN­otE­xist)
HttpRe­spo­nse­Red­irect
takes a single argument: the URL to which the user will be redire­cted# Always return an HttpRe­spo­nse­Red­irect after succes­sfully dealing # with POST data. This prevents data from being posted twice if a # user hits the Back button.