41
Deploying GeoDjango applications on heroku: Easy workthrough
Recently, I was in the position to quickly prototype a location-based application. Since Django is pretty awesome for quick scaffolding with all other requirements still intact, I went straight for it using its extenstion GeoDjango1. The development process was pretty interesting 😌. However, deploying the prototyped application on Heroku was a huge pain 😢 since resources were sparse and those available were seemingly outdated.
This guide tends to provide an updated workthrough to overcoming this hurdle.
It is assumed that you have a GeoDjango1 application available for deployment. If not, go through 1 or Make a Location-Based Web App With Django and GeoDjango2. Another assumption is that you have chosen to host your application on Heroku and your database is PostgreSQL.
Let's get started. Having configured your application for Heroku deployment3, append the following to your settings.py
file.
...
DATABASES['default']['ENGINE'] = 'django.contrib.gis.db.backends.postgis'
If you follow Django Tutorial Part 11: Deploying Django to production3, it should come directly after:
# Heroku: Update database configuration from $DATABASE_URL.
import dj_database_url
db_from_env = dj_database_url.config(conn_max_age=500)
DATABASES['default'].update(db_from_env)
The added code instructs Heroku to use a spatial database extension called postgis. In most cases, failure to include that line results in this error:
AttributeError: 'DatabaseOperations' object has no attribute 'geo_db_type'
Next, we need to add a custom buildpack to our project on heroku. By default, all Python applications are automatically given heroku/python
official buildpack. However, since our application needs more than just a python
runtime environment — our application needs GDAL
, GEOS
, and PROJ
in addition to a python runtime environment — we must get the additional environment in place for our application to compile. There are various buildpacks out there for this, however Heroku Buildpack Geo worked seemlessly.
NOTE: It should be noted that this buildpack only provides the runtime environments for GIS
related projects therefore, it cannot be used alone. You still need a python runtime environment for your applications to run.
Doing this is pretty easy with Heroku since it supports multiple buildpacks.
To add this custom buildpack — after you created your heroku application using heroku create application_name
— follow the steps below:
-
Set the official python buildpack:
heroku buildpacks:set heroku/python
-
Insert the custom
heroku-geo-buildpack
:
heroku buildpacks:add --index 1 https://github.com/heroku/heroku-geo-buildpack.git
This will insert the
heroku-geo-buildpack
buildpack at the first position in the order of buildpack execution, and move the other buildpacks that are ahead of it down one position.4
NOTE: Ensure heroku-geo-buildpack
buildpack comes first! It installs the basic requirements of a GIS
application.
You can confirm it has been included by issuing the following command in your terminal:
heroku buildpacks
You can now happily deploy your application 😊㊗️🎈.
Ran into some issues? The comment session is live 🤗.
-
GeoDjango Tutorial - by Official Django Docs ↩
-
Make a Location-Based Web App With Django and GeoDjango - by Ahmed Bouchefra ↩
-
Django Tutorial Part 11: Deploying Django to production - by MDN ↩
-
Using Multiple Buildpacks for an App - by Heroku ↩
41