Advertisements
Skip to content

February 20, 2018

DJANGO Cheat Sheet

by Criss Davis

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

linux:
virtualenv --python=python3.6 venv
windows:
virtualenv venv

linux:
source venv/bin/activate

windows:
\path\to\env\Scripts\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 manage.py runserverall

# Create a superuser
python3 manage.py createsuperuser [username]

# Test Server
python3 manage.py runserver

Creating a Django App

1. Open terminal and navigate to django project folder (where manage.py 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 urls.py and add “from [appname] import views”. Then add url path for new view.
e.g. url(r’^$’, views.home, name=‘home’)
9. Open views.py 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 settings.py. 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 settings.py and scroll down to DATABASES section. Replace with:

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

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

Advertisements
Read more from DJANGO

Leave a Reply

%d bloggers like this: