Learn Django

Ready Local Environment

In order to use Heroku you need to create an account with them, install some additional software on your computer, and update a few settings. Once these steps are completed you'll be setup to create and manage applications on Heroku from the command line.

Follow these steps to get it setup.

Step 1: Create a Heroku Account

Go to heroku.com to create an account now, and then come back to this lesson.

Step 2: Install Heroku toolbelt

Heroku provides a utility kit that makes it easy to use their service from the command prompt/terminal. Follow these steps to install it.

  1. Open Heroku Toolbelt
  2. Click on the download button for your OS
  3. Run the .pkg or .exe file and follow the instructions
  4. Once installed, open a terminal, navigate to the crmeasy folder, activate the virtualenv, and execute these commands
1
2
3
4
5
6
7
8
(venv)$ heroku login
Enter your Heroku credentials.
Email: tobias@example.com
Password:
Could not find an existing public key.
Would you like to generate one? [Yn]
Generating new SSH public key.
Uploading ssh public key .ssh/id_rsa.pub

Step 3: Install Django-Toolbelt

Heroku created utility package that contains many of the common components involved in a Django project. Some of these components have already been installed; however a few haven't. Run this command to install the others.

1
2
3
4
5
(venv)$ pip install django-toolbelt
Installing collected packages: Django, psycopg2, gunicorn, dj-database-url, dj-static, static
  ...
Successfully installed Django psycopg2 gunicorn dj-database-url dj-static static
Cleaning up...

Step 4: Create a Procfile

According to Heroku's documentation, a Procfile is "a mechanism for declaring what commands are run by your application’s dynos on the Heroku platform". In our project we want Heroku to run the 'collectstatic' command. This command is used to gather all static files and put them in a location that Heroku can then serve them from.

  1. Using your terminal, navigate to /.../crmeasy
  2. Create the Procfile file and edit it using the following steps

Create the Procfile on a Mac

1
2
# create and open the file
(venv)$ vi Procfile
  1. Press i
  2. Enter this text in the file
    web: python manage.py collectstatic --no-input; gunicorn crmapp.wsgi
  3. Press the esc key
  4. Type :wq and press the enter key

Create the Procfile on Windows

1
2
# create and open the file
(venv) C:\ notepad Procfile

Then enter this text into the file and save it:

web: python manage.py collectstatic --noinput; gunicorn crmapp.wsgi

Step 5: Create a Runtime File

Heroku uses a file named runtime.txt to determine which version of Python to use for the application. These steps will show you how to create this file.

  1. Using your terminal, navigate to /.../crmeasy
  2. Create the runtime file and edit it using the following steps

Create the Runtime file on a Mac

1
2
# create and open the file
(venv)$ vi runtime.txt
  1. Press i
  2. Enter python-2.7.9
  3. Press the esc key
  4. Type :wq and press the enter key

Create the Runtime file on Windows

1
2
# for Windows
(venv) C:\ notepad runtime.txt

Then enter this text into the file and save it:

python-2.7.9

Step 6: Freeze Requirements

Per Heroku's documentation, they recognize Python applications by the existence of a requirements.txt file. This file specifies modules and their versions that are to be used in the application. This file can be generated from PIP by executing the following command.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
# make sure you're in the crmeasy directory
(venv)$ pwd
/.../crmeasy

# freeze requirements
$ pip freeze > requirements.txt

# view requirements.txt contents
$ cat requirements.txt 
Django==1.7.1
dj-database-url==0.3.0
dj-static==0.0.6
django-toolbelt==0.0.1
gunicorn==19.1.1
psycopg2==2.5.4
static3==0.5.1
wsgiref==0.1.2

Step 7: Commit Changes

Execute these commands to commit your changes in Git.

1
2
3
4
5
# add files
(venv)$ git add .

# commit files
(venv)$ git commit -m "getting env ready for push to heroku"

Track your progress with a free account