Skip to content

February 20, 2018

DJANGO Cheat Sheet

by Criss Davis

Set up the virtual environment and create a new project in DJANGO.

virtualenv --python=python3.6 venv
virtualenv venv

source venv/bin/activate


pip3 install django
pip3 install pillow
django-admin startproject [project name]

Verify project creation and start DJANGO server.

# Navigate to the newly created project folder.
. python3 runserverall

# Create a superuser
python3 createsuperuser [username]

# Test Server
python3 runserver

Creating a Django App

1. Open terminal and navigate to django project folder (where exists)
2. django-admin startapp [app name] # use pluralised names, e.g. posts
3. Navigate to new app folder.
4. Create new subfolder called “templates”
5. Navigate to new templates folder.
6. Create new subfolder called same name as your app (e.g. posts)
7. Go back to project folder
8. Open project and add “from [appname] import views”. Then add url path for new view.
e.g. url(r’^$’, views.home, name=‘home’)
9. Open and add:
def home(request):
return render(request, ‘posts/home.html’)
10. Navigate to projectfolder\appfolder\templates\appfolder
11. Create new file ‘home.html’
12. Go to project folder and open Scroll down to INSTALLED_APPs and add
‘appname’, to the end of the list of apps.

Connecting PostgreSQL to a Django Project

1. Create a new database in PostgreSQL, and assign a DB user with admin privileges
2. Go to project and scroll down to DATABASES section. Replace with:

 'default': {
 'ENGINE': 'django.db.backends.postgresql',
 'NAME': ‘[databasename]’,
 'USER': ‘[username]’,
 'PASSWORD': ‘[password]’,
 'HOST': '',
 'PORT': '5432',

3. Scroll down to TIMEZONE and replace code. e.g. TIME_ZONE = ‘Australia/Perth’

Read more from DJANGO

Leave a Reply

%d bloggers like this: