Learn Django

Push to Heroku

At this point everything is ready to be deployed to Heroku. You'll see this commonly referred to as a 'push'. It's called a push because that's the git command used to deploy application code. Follow these steps to deploy your code.

Step 1: Create Heroku App

With the repository created, it's now time to create the Heroku app. This is a simple process that the Herorku toolbelt helps with.

  1. In the /.../crmeasy directory, execute this command
1
2
3
4
(venv)$ heroku create
Creating glacial-dawn-9026... done, stack is cedar
http://glacial-dawn-9026.herokuapp.com/ | git@heroku.com:glacial-dawn-9026.git
Git remote heroku added

In this example, glacial-dawn-9026 is the name of the Heroku application. Your application will have a different name.

Step 3: Set Environment Variables on Heroku

Now set the environment variables by typing these commands into the terminal. This will tell Django to use the production settings.

On a Mac, you can get the SECRET_KEY variable by typing cat ~/.bash_profile at the terminal. On Windows you can type set at the command prompt.

1
2
$ heroku config:add ENV_ROLE=production
$ heroku config:add SECRET_KEY=<your secret key>

Step 2: Push to Heroku

Now that the Django app and Heroku app have been created, it's time to push the code to Heroku. Follow these steps to do so.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
(venv)$ git push -f heroku push_to_heroku:master
Initializing repository, done.
Counting objects: 89, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (83/83), done.
Writing objects: 100% (89/89), 77.52 KiB | 0 bytes/s, done.
Total 89 (delta 33), reused 0 (delta 0)

-----> Python app detected
-----> Preparing Python runtime (python-2.7.4)
-----> Installing Setuptools (3.6)
-----> Installing Pip (1.5.6)
-----> Installing dependencies using Pip (1.5.6)

       (dependency installation)

       Successfully installed Django South dj-database-url dj-static django-toolbelt gunicorn psycopg2 pystache static
       Cleaning up...
-----> Preparing static assets
       Running collectstatic...
       80 static files copied to '/app/staticfiles'.

-----> Discovering process types
       Procfile declares types -> web

-----> Compressing... done, 32.0MB
-----> Launching... done, v7
       http://powerful-citadel-9773.herokuapp.com/ deployed to Heroku

To git@heroku.com:powerful-citadel-9773.git
 * [new branch]      push_to_heroku -> master

Step 4: Commit the Initial Migration on the Heroku App

The database on Heroku will be different than that on your local machine. Said differently, if you create a user, or add information in your development environment it won't show up on Heroku. Therefore you'll need to execute the same database commands in production as you did on your local dev machine. Execute this command now.

1
2
3
4
5
6
7
8
9
(venv)$ heroku run python manage.py migrate
Running `python manage.py migrate` attached to terminal... up, run.9550
Operations to perform:
  Apply all migrations: contenttypes, auth, admin, sessions
Running migrations:
  Applying contenttypes.0001_initial... OK
  Applying auth.0001_initial... OK
  Applying admin.0001_initial... OK
  Applying sessions.0001_initial... OK

Step 5: Create the Superuser on the Heroku App

Now add the superuser on Heroku.

1
2
3
4
5
6
7
# create the superuser
(venv)$ heroku run python manage.py createsuperuser
Username: example
Email address: example@example.com
Password: 
Password (again):
Superuser created successfully.

Step 6: Open the Heroku App

With all previous steps complete, you should now be able to open your Heroku app. Type in the following command to do so.

1
2
# open the heroku app
(venv)$ heroku open  --app <insert your heroku app name>
Track your progress with a free account