Develop on Ubuntu; Source on Github; Deploy to Heroku
You may already have (some of) these installed. It is best not to install Django system-wide, to avoid confusion with the Django instance that will be installed inside your virtual environment below.
$ sudo apt-get install python python-pip python-virutalenv git
# Install Heroku via PPA
$ wget -qO- https://toolbelt.heroku.com/install.sh | sh
Create a new repo the usual way. Check the "Initialize this repository with a README" box and choose a "Django" .gitignore file from the list.
Clone the repo to your local computer.
$ git clone [email protected]:jmcvetta/myproject.git
Setup and activate virtualenv.
$ cd myproject
$ virtualenv --no-site-packages venv
$ source venv/bin/activate
(venv
for virtualenv. That is the conventional name, but you can choose whatever you like.)
Install some things into your virtualenv.
$ pip install Django psycopg2 south pillow dj_database_url ipython gunicorn django-bcrypt
- psycopg2 is a PostgreSQL adapter for Python.
- South handles database migrations.
- Pillow is a virtualenv-safe replacement for PIL.
- dj_database_url makes DB settings for Heroku easy.
- IPython provides an enhanced Python shell for your development pleasure.
- Gunicorn 'Green Unicorn' is a Python WSGI HTTP Server for UNIX.
- django-bcrypt provides more secure password encryption using bcrypt.
Now save the exact version into a requirements.txt
file.
$ pip freeze > requirements.txt
Start a Django project
$ django-admin.py startproject myproject .
Add south
and gunicorn
to your INSTALLED_APPS
in settings.py
.
INSTALLED_APPS = (
...
'south',
'gunicorn',
'django_bcrypt',
)
Configure settings to use Heroku's Postgres database when deployed, and SQLite locally.
import dj_database_url
import os
DATABASES = {'default': dj_database_url.config(default='sqlite:///' +
os.path.join(os.path.dirname(os.path.abspath(__file__)), '..', 'db.sqlite') )}
Create a Procfile
to to launch the web process using Gunicorn.
# Note the single quote marks so $PORT gets echoed literally
$ echo 'web: gunicorn myproject.wsgi -b 0.0.0.0:$PORT' > Procfile
Go make something awesome!
$ heroku create
$ git push heroku master
$ heroku run ./manage.py syncdb
$ heroku open
Update your database with South and push the changes to Heroku
- Make changes to your models
$ python ./manage.py schemamigration [appname] --auto
$ python ./manage.py migrate [appname]
- [commit & push changes to heroku]
$ heroku run ./manage.py migrate [appname]
Whenever you work on your project, you'll want to activate your virtualenv:
$ source venv/bin/activate
Install new packages with Pip, then update requirements.txt
file.
$ pip install django-nose
$ pip freeze > requirements.txt
$ git commit requirements.txt -m "Added django-nose to requirements.txt"
If requirements.txt
was updated elsewhere, you can update your virtualenv:
$ pip install -r requirements.txt
Sync and/or migrate your database:
$ python ./manage.py syncdb
$ python ./manage.py migrate [appname]
Finally, fire up your server:
$ python ./manage.py runserver