Basic commands for working with Django

April 19, 2020

Recently I've started working a lot with Django (this website plus a few other projects I have in the works), but I am always forgetting a couple of the commands and have to look them up.

So I thought I would gather the commands I forget together (plus the other basic commands for completion) so I have a place to look them up quickly, and thought they might be useful to others as well.



Virtual environments

Creating a virtual environment:
python -m venv <path>

Activate the virtual environment:
source <path>/bin/activate



Django basics

Install Django
pip install Django

Create a project:
django-admin startproject <project name>

Create an app:
python manage.py startapp <app name>



Migrations

Prep the DB migration:
python manage.py makemigrations <(optional) list of apps>

Migrate the DB changes:
python manage.py migrate <(optional) list of apps>



Users

Creating the super user:
python manage.py createsuperuser

Reset a user's password:
python manage.py changepassword <username>



Other Commands

Run the project:
python manage.py runserver <(optional) server details>
Server details can just be the port (default 8000), or address and port in the form 127.0.0.1:8000 - localhost is also a valid value for address.

Run the shell:
python manage.py shell