Django development on Heroku
Like most stories, this one starts from the beginning - I'll be posting about Django, Heroku and eventually Amazon S3 while I'm building this blog. You won't be able to find this post for now because I don't even have a domain yet; yesteday I didn't even have a frontend :)
That's the amazing thing about Django and Heroku - in just a couple of days, for a grand total of $0, I already have a working custom web application. It can scale to arbitrary sizes, it is secure and most importantly - fully and easily customizable.
What I already have working:
- Debian Unstable (Virtual Machine)
- Python 2.7
- vim with auto-completion and syntax highlighting
- venv (isolated Python environments)
- Django 1.4
- Heroku Toolbelt
- PostgreSQL 9.x
- an excellent CC-licensed theme
- django-storages and boto (for Amazon S3)
The reason for the Debian VM is because I found Django development very difficult on Windows, psycopg2 (the PostgreSQL interface) wouldn't compile and after trying to troubleshoot it for a while I gave up, started Linux, `aptitude install`ed the needed stuff along with some extras like IPython and launched my app for the first time.
My next issue was the development environment, I thought I could just use IDLE, but found that auto-completion doesn't work - it worked just for Python keywords, but not for functions and objects imported from libraries like Django. I think this was caused by the virtual environment. I found out that vim is a very good Python IDE, with most extras I'm used to from Visual Studio (vim settings guide, parts of another guide). I have vim-gtk installed, even though I ended up using it from the console. The package is built with Python plug-ins support.
I installed git-gui and gitk to have graphical commit and repository tree views. Again, no issues, everything worked out of the box.
To resume development from another PC, you need to create a key and clone your repository. You can then initialize a virtual environment in the `venv` folder with `virtualenv venv`, this will also make pip available. Activate the environment and go to your source code folder, then install everything needed with `pip install -r requirements.txt`. It's recommended you do all this in a new folder, so you have a single folder for your project with two sub-folders - the source and the virtual environment for Python.
Next to configure PostgreSQL on the development computer you can install it with `aptitude install postgresql`, change to its user with `sudo su - postgres`, execute `createuser -P django` to create a new user named "django", then enter psql and create the database - `CREATE DATABASE django_db OWNER django ENCODING 'UTF8';`
With dj_database_url you have to set an environment variable that tells settings.py where to look for your database server. In my case the full command is `export DATABASE_URL="postgres://django:djangopassword@localhost/django_db"`. You can add this to `venv/bin/activate` to apply it automatically every time you enter the virtual environment.
If you're using South like me, you can restore all your tables with `python manage.py syncdb` and `python manage.py migrate`.