Heroku is a very popular Cloud PaaP (Platform as a Service) platform which allows you to deploy various applications. Heroku doesn’t charge anything to signup and has a free service upto 5 MB usage of database, that’s enough to try it out.
Heroku is majorly to Deploy Ruby, Node.js, Clojure, and Java apps. But, you can also run any type of app (like Django). In this post I’ll explain how to deploy Django project on Heroku.
I assume that you already signed-up with Heroku and configured your SSH keys with Heroku servers. If you haven’t done it already, follow these instructions – http://devcenter.heroku.com/articles/quickstart
EDIT:
Sep 29th, 2011 – Heroku now officially supports Django – http://devcenter.heroku.com/articles/django
Sep 17th, 2011 – Added `gunicorn` to make the django project production ready.
OK, Let’s get started -
Create a folder for heroku app:
mkdir heroku-django
cd heroku-django
Create a virtualenv inside the heroku app folder and activate it:
virtualenv --no-site-packages .
source bin/activate
Install django and psycopg2 packages
bin/pip install django
env ARCHFLAGS="-arch i386 -arch x86_64" bin/pip install psycopg2
bin/pip install gunicorn
Save all the local site-packages to requirements.txt file
bin/pip freeze > requirements.txt
Create a new django project naming ‘django_project’
bin/django-admin.py startproject django_project
Edit django_project/settings.py file to Uncomment admin app of our django project:
vi django_project/settings.py
# After Uncommenting .admin and .admindocs app INSTALLED_APPS in settings.py looks like this:
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.admin',
'django.contrib.admindocs',
)
Create a Process file for heroku to run:
vi Procfile
Add these two lines:
web: bin/gunicorn_django --workers=4 --bind=0.0.0.0:$PORT django_project/settings.py
worker: bin/python django_project/manage.py celeryd -E -B --loglevel=INFO
Edit django_project/urls.py to add a controller for homepage and admin panel on django:
vi django_project/urls.py
Your urls.py file will have these lines after edit:
from django.conf.urls.defaults import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^$', 'views.home', name='home'),
url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
url(r'^admin/', include(admin.site.urls)),
)
Create a view for homepage:
vi django_project/views.py
Add these lines to views.py file, it just returns ‘Hello World!’ http response in plain text.
from django.http import HttpResponse
def home(request):
return HttpResponse("Hello World!")
Add virtualenv folders to .gitignore list:
cat >.gitignore <<EOF
bin/
include/
lib/
EOF
Initialize a git repository and do initial commit:
git init
git add .
git commit -m "Initial Commit"
Create a heroku app with cedar stack, note that this command automatically sets the remote server as heroku with the url to new heroku app’s git repository:
heroku create --stack cedar
# just to verify the heroku remote
git remote show heroku
Your Heroku Folder will look like this (excluding virtualenv files):
.Python
.gitignore
Procfile
django_project/__init__.py
django_project/manage.py
django_project/settings.py
django_project/urls.py
django_project/views.py
readline-6.2.1-py2.6-macosx-10.6-universal.egg/readline.so
requirements.txt
Create (or Sync) database and tables:
heroku run bin/python django_project/manage.py syncdb --app vivid-water-6038
Now, Push the django files, Procfile and requirements.txt to Heroku (master branch):
git push heroku master
Here, replace vivid-water-6038 with your heroku app name.
Congratulations! you’ve just created your first django app on Heroku:
Access from http://your-app-name-here.herokuapp.com
My Heroku app:
http://vivid-water-6038.herokuapp.com
Have fun! :)
Thanks!
Reference:
https://gist.github.com/1004844
Join the Hacker News Discussion:
http://news.ycombinator.com/item?id=3008106





Pingback: Quora