Skip to content

Instantly share code, notes, and snippets.

@dwayne
Last active February 23, 2017 18:49
Show Gist options
  • Save dwayne/f3d65b19356fca24b05b to your computer and use it in GitHub Desktop.
Save dwayne/f3d65b19356fca24b05b to your computer and use it in GitHub Desktop.
Django Notes (Python 3.5.1 and Django 1.9.x)

Getting Started

Install Python, follow the steps here.

Steps I take when beginning a new Django project.

$ mkdir [project_name]_project
$ cd [project_name]_project

$ ~/local/python-3.5.1/bin/pyvenv env
$ . env/bin/activate

$ pip install django
$ pip freeze > requirements.txt

$ django-admin startproject [project_name]

$ touch .gitignore

Contents of the .gitignore file.

db.sqlite3
env
*.pyc

Before making anymore changes test that it works and then put everything under version control.

# Test it out
$ cd [project_name]
$ python manage.py migrate
$ python manage.py runserver
# View the site at http://127.0.0.1:8000/

# Version it
$ git init
$ git add --all
$ git commit -m "Initial commit"

Now change the database and timezone settings in [project_name]_project/[project_name]/[project_name]/settings.py.

Prerequisites

  1. Install psycopg2 with $ pip install psycopg2. See here for more details.
  2. Create the database in PostgreSQL, say [project_name]_development, with $ createdb -O [user] [project_name]_development.
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': '[project_name]_development',
        'USER': '[user]',
        'PASSWORD': '********',
        'HOST': 'localhost',
        'PORT': '5432'
    }
}

TIME_ZONE = 'America/Port_of_Spain'

Tip: If you want to connect without specifying a user and password then use the following setting.

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': '[project_name]_development'
    }
}

Without specifying a HOST psycopg2 will connect using a Unix socket in the same manner as psql. When you specify a HOST, psycopg2 will connect with TCP/IP, which requires a password. (ref)

Now, we can migrate the db, create a super user, start the server and view the admin page to see that everything was set up correctly.

$ python manage.py migrate
$ python manage.py createsuperuser
$ python manage.py runserver

View the admin page at http://localhost:8000/admin.

Update your requirements.txt file and finally commit your changes at this point.

$ pip freeze > requirements.txt
$ git add --all
$ git commit -m "Configure a PostgreSQL database and set the timezone"

Heroku

This covers the changes that need to be made to the project to deploy to Heroku.

Install and configure the Heroku Toolbelt.

Install gunicorn

$ pip install gunicorn

and add a Procfile.

web: cd [project_name] && gunicorn [project_name].wsgi --log-file -

Update the database configuration to use an environment variable $DATABASE_URL.

$ pip install dj_database_url
# Update database configuration with $DATABASE_URL.
import dj_database_url
db_from_env = dj_database_url.config(conn_max_age=500)
DATABASES['default'].update(db_from_env)

Place the following in .env in [project_name]_project. Add to .gitignore as well.

DATABASE_URL=postgres:///hello_world_development

Update your static files settings.

PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__))

STATIC_ROOT = os.path.join(PROJECT_ROOT, 'staticfiles')
STATIC_URL = '/static/'

# Extra places for collectstatic to find static files.
STATICFILES_DIRS = (
    os.path.join(PROJECT_ROOT, 'static'),
)

Add staticfiles to .gitignore as well. Here's the updated .gitignore.

db.sqlite3
env
staticfiles
.env
*.pyc

Set up Django to serve static files in production.

Install whitenoise.

$ pip install whitenoise

In settings.py add the following,

# Simplified static file serving.
# https://warehouse.python.org/project/whitenoise/

STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage'

Then, in wsgi.py do the following,

import os
 
from django.core.wsgi import get_wsgi_application
from whitenoise.django import DjangoWhiteNoise
 
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "hello_world.settings")
 
application = get_wsgi_application()
application = DjangoWhiteNoise(application)

Test it all out.

# Test static files collection, ensure when you deploy that this would run smoothly
$ python manage.py collectstatic --noinput

# Test running the app locally
$ heroku local web

Freeze your requirements.

$ pip freeze > requirements.txt

Tell Heroku that you're using Python 3.5.1. Add a runtime.txt file with the following contents.

python-3.5.1

Here are the supported Python versions.

References

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment