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.
... 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
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 buildpacks:add --index 1 https://github.com/heroku/heroku-geo-buildpack.git
This will insert the
heroku-geo-buildpackbuildpack at the first position in the order of buildpack execution, and move the other buildpacks that are ahead of it down one position.4
heroku-geo-buildpack buildpack comes first! It installs the basic requirements of a
You can confirm it has been included by issuing the following command in your terminal:
You can now happily deploy your application 😊㊗️🎈.
Ran into some issues? The comment session is live 🤗.
- Cover image: https://www.google.com/url?sa=i&url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DR9ZJZtHY7RY&psig=AOvVaw1PEbujb86dE7nkyQJAv5gU&ust=1624852485065000&source=images&cd=vfe&ved=0CAwQ3YkBahcKEwiw6ee39bbxAhUAAAAAHQAAAAAQAw
Make a Location-Based Web App With Django and GeoDjango - by Ahmed Bouchefra ↩