Tuesday, May 15, 2012

Deploying Django Apps to Heroku (with existing git repository)


So basically i decided to write this tutorial based on this blog and the official tutorial.

The main difference (apart from putting the best of the 2 together) is that this tutorial covers django deployment to heroku assuming that you already have a GIT repository with your remote references (bitbucket , github , etc).

By doing this tutorial the heroku remote reference will be added automatically to your .git , so you will have 2 remote references and you can choose where you want to push to. You can  then git push origin master or git push heroku master.

The above links explain in detail the meaning of every actions, i am merely stating the commands i used in my case (that hopefully will be yours) to deploy my app.

Prerequisites :

- Heroku account
- Heroku command line client
- Python 2.7, Django 1.4, virtualenv, pip , git
- Must use pip to manage dependencies with a requirements.txt file

I am using Ubuntu 12.04 LTS. I have all the dependencies asked by installing them through this tutorial . Make sure you have Django installed in your virtualenv using pip install Django psycopg2

This example uses a repository for one of the projects i was working on, change it according to your needs.


1 . git clone https://bitbucket.org/psychok7/human-computer-interaction.git
2 . cd human-computer-interaction/
3 . virtualenv venv --distribute  #you need to create because its not on .git
4 . source venv/bin/activate
5 . pip freeze > requirements.txt   #add the rest of the dependencies by hand like for example Django==1.4 , psycopg2==2.4.5 if it doesn't create automatically

#install dependencies in case you need them
  pip install -r requirements.txt
#add the remote references only if necessary (for example cloning in another computer) using
   git remote rm heroku
   git remote add heroku git@heroku.com:newname.git
#Exclude unnecessary env files from git by creating a .gitignore file and adding env and *.pyc on it

6 . git add .
7 . git commit -m "my django app"    #commit to git
8 . heroku create --stack cedar
9 . git push heroku master

#if you get errors check the log for missing dependencies, etc. then add the dependencies to requirements.txt

10 . heroku run python fastparqsite/manage.py syncdb

#everything should be up and running now

Useful tips:

1 . heroku pg:reset SHARED_DATABASE #delete remote database
2 . chmod 755 fastparqsite/manage.py (and commit it) #if you get a permission denied error this might be useful
3 . pip install -r requirements.txt #install dependencies on local machines after cloning
4 . use guarnicon webserver instead of the default one in heroku (guarnicon is better)
5 . Make sure your settings.py has its relative paths properly set, see this example if not you might run into errors.


  1. if you are cloning an existing heroku project:
    1 . git clone https://bitbucket.org/psychok7/human-computer-interaction.git
    2 . cd human-computer-interaction/
    3 . virtualenv venv --distribute #you need to create because its not on .git
    4 . source venv/bin/activate
    5 . pip install -r requirements.txt
    6 . git remote add heroku git@heroku.com:newname.git

    1. if on a different computer and trying to clone from heroku you should heroku keys:add first than clone it otherwise you might get a permission denied

  2. for step 10 we can do this in a 1.4 project 10 . heroku run python manage.py syncdb

  3. I using south migrations, after heroku run python manage.py syncdb , you should heroku run python manage.py migrate

  4. Using South in an Existing App
    In many case you may need to install South after working on an app a bit. In that case you have to first convert you existing app to a app using South. You have to perform the following steps:

    Install South and add south to your INSTALLED_APPS in settings.py
    Run python manage.py syncdb
    Run ./manage.py convert_to_south web
    That’s it.

    If you are working in a team and using any version control system, you have to commit this migration. And every other member have to perform a syncdb and then run
    ./manage.py migrate
    on their machine after pulling code.

    But if you are using a fresh installation of the same codebase, you just have to run syncdb and a normal migration.

  5. Check app status and erros

    heroku ps
    heroku logs --tail