Django cheat sheet

Creating a base project: django-admin startproject projectname

New app within existing project: python3 manage.py startapp appname

You’ll want to add the appname to the end of the list of INSTALLED_APPS in projectname/settings.py

Django uses Model View Controller pattern – URL Pattern maps to Views, which return HTML and can make use of Models and Templates.

https://docs.djangoproject.com/en/4.0/topics/db/models/

Migrations – generate scripts to change our existing database structure as we make updates & changes. Used when you add a new model, or add, remove, or change fields on an existing model.

Migration commands:

python3 manage.py makemigrations

Generates files for later use using current model fields & database tables. Creates numbered files in migrations folder. (if made but hasn’t been run yet, it’s unapplied; common error so use migrate command below)

python3 manage.py showmigrations

python3 manage.py migrate <appname> <number>

appname and number are OPTIONAL, and without them it runs all migrations that haven’t yet been run. Using them lets you run migrations in a specific app up to a specific number.

python3 manage.py createsuperuser

Use HTML templates for pages. Templates can inherit from other templates, and can have code (including javascript files) injected into them.

Leave a comment