How to deploy a Django app on Heroku. Part II

This is the second part of a series on how to deploy a Django app on Heroku. If you haven’t looked at the first part, you might want to do it now. Go to How to deploy a Django app on Heroku. Part I.

You will see that the main idea behind these posts is not to build a functional Django app. Instead, what I want to do is to give you a bunch of good practices on how to build a Django app with different environments for testing, developing and production, useful packages installed, Internationalization and Localization properly configured, PosgreSQL installed, and much more.


The topics covered here are:

  • set different files for development, production and testing
  • save all the templates of your app in the same location
  • set debug false in production
  • install HTML5 Boilerplate

Django settings – Advanced configuration

On thing you might want is to have different files for development, production and even testing.

We leave the normal file for production (on Heroku) and we define two different files for development and testing. These files are called and and are located at the same level of


These files will contain the settings that are specific to this environment, but also import all the global settings (which in this case is the file Write, in each of these files:

# -*- coding: utf-8 -*-

from .settings import *

DEBUG = True


Note: the dot before settings is not a typo 🙂

Then, on the virtual environment used for development, append this to your bin/activate script:



where myproject is the name of your main project. In the script bin/activate of the virtual environment for testing:



OS X Mavericks Error: When working with these virtual environments, if you run

$ python validate

you might see an error with something like:

Referenced from: myvirtualenv/lib/python2.7/site-packages/psycopg2/

Reason: image not found

To solve this error, open the bin/activate script and add the following line:

export DYLD_LIBRARY_PATH=/Library/PostgreSQL/9.*/lib

where you should replace * for your PostgreSQL version.

Templates App

Another useful thing is to have all your templates in the same place. You can accomplish this by creating a Django app and use it to contain the templates and the template tags. Go to your project folder, at the same level of the file, and type:

$ python ../ startapp mytemplates

This way, this app will be inside the myproject folder. Finally, you should include this app in the file:


In mytemplates folder, where you have the usual files of a Django app, create a templates folder

$ mkdir templates

Inside this folder you should place all your project templates. A good practice is to create a different folder for each app on your project. Moreover, the templates folder will contain the error pages: 403.html, 404.html and 500.html.

$ cd templates

$ touch 403.html 404.html 500html

For now edit them and write something like “Error page 403” (if you have more time you can make them nicer…). In this folder, you can also place your base.html template, that will be used throughout your application.

Note: if you want to create template tags, I recommend placing them on a new folder inside your mytemplates app, at the same level of the templates folder, called templatetags.

Setting debug False on production

In the production environment, in Heroku, we don’t want to use the debug mode. Therefore, in your file, change

DEBUG = False


and then, upload the application on Heroku

heroku login

git add .

git commit – m “Different files”

git push heroku master

With this configuration, if you followed Part I of this post, you should see the error message you put on the 404.html template. This is because your don’t have yet your file configured.

HTML5 Boilerplate

If you want to create a nice and responsive Django app, I recommend you install HTML5 Boilerplate. Go to the webpage and click the Download button to download a zip file (in my case, it was the version 4.3.0).

Once in your computer, unzip the file and place its contents inside myproject/mytemplates/templates folder. Note that you should override the file 404.html. Moreover, I usually change the name of the file “index.html” to “base.html“, but it’s up to you.

However, the two folders js and css, and the two images favicon.ico and apple-touch-icon-precomposed.png, contain or are static files. Go to the projects folder, at the same level of the file, and create a static folder:

$ mkdir static

$ mv mytemplates/templates/{js,css,favicon.ico,apple-touch-icon-precomposed.png} static/

Then, open the base.html file, inside mytemplates/templates, and change the urls that point to these two folders. For example, you should change the lines

<link rel=“stylesheet” href=“css/normalize.css”>
<link rel=“stylesheet” href=“css/main.css”>

<script src=“js/vendor/modernizr-2.6.2.min.js”></script>

<link rel=“stylesheet” href=“{{STATIC_URL}}css/normalize.css”>
<link rel=“stylesheet” href=“{{STATIC_URL}}css/main.css”>

<script src=“{{STATIC_URL}}js/vendor/modernizr-2.6.2.min.js”></script>

and so on. To include the icon images write in the header:

<link rel=”icon” type=”image/png” href=”{{STATIC_URL}}apple-touch-icon-precomposed.png”>

<link rel=“shortcut icon” href=“{{STATIC_URL}}favicon.ico” type=“image/x-icon”>

Moreover, I usually comment the script for Google Analytics to use it later.

To see the effects and assure that everything works, edit the myproject/ file:

urlpatterns = patterns(”,

    url(r’^$’, ‘myproject.views.home’, name=’home’),


Then, edit or create the myproject/ file:

# -*- coding: utf-8 -*-
from django.shortcuts import render_to_response
from django.shortcuts import RequestContext
def home(request):
    return render_to_response(“base.html”RequestContext(request, {}))

Note: the first line indicates the coding to use. It is a good practice to insert this line at the beginning of the files of your django application, like,,, and

Moreover, to see more clearly if the static files are rendered, edit the myproject/static/css/main.css file and include:

body {

    background-color: #DAF;


Try your Django project locally, with foreman start, to see if everything works as expected. You should see the text “Hello World! This is HTML5 Boilerplate.” and a purple background.

Then, upload the application on Heroku:

heroku login

git add .

git commit – m “HTML5 Boilerplate added”

git push heroku master

Hopefully, you will see the same text and background on Heroku 🙂

This is the end of this post. The third part is available here –> Part III

Please, click on the g+1 if you found this post useful! 🙂

Recommendations and comments are welcome 🙂


Please, add +Marina Mele in your comments. This way I will get a notification email and I will answer you as soon as possible! :-)