DEV Community

Cover image for Deploying GeoDjango applications on heroku: Easy workthrough

Deploying GeoDjango applications on heroku: Easy workthrough

John Owolabi Idogun on June 27, 2021

Motivation Recently, I was in the position to quickly prototype a location-based application. Since Django is pretty awesome for quick s...
Collapse
 
mojemoron profile image
Micheal Ojemoron • Edited

battling serious issues with deploying to heroku: dev-to-uploads.s3.amazonaws.com/up...

Collapse
 
sirneij profile image
John Owolabi Idogun

Worked locally?

Collapse
 
mojemoron profile image
Micheal Ojemoron

yes

Thread Thread
 
sirneij profile image
John Owolabi Idogun

Did you put GDAL buildpack before python's? Did you configure your production database as specified in the post?

Thread Thread
 
mojemoron profile image
Micheal Ojemoron

yes I did,
dev-to-uploads.s3.amazonaws.com/up...
is there a way we can have a google meet, i would be very grateful for this, i have spent the week debugging the issues

Thread Thread
 
sirneij profile image
John Owolabi Idogun

Before doing that, do you mind sending links to the screenshots of your settings.py? Ensure all sensitive data are properly hidden.

Thread Thread
 
sirneij profile image
John Owolabi Idogun

Also, can you let me see the output of heroku buildpacks?

Thread Thread
 
mojemoron profile image
Micheal Ojemoron

thats the first link i sent: dev-to-uploads.s3.amazonaws.com/up...

Thread Thread
 
mojemoron profile image
Micheal Ojemoron

from sentry_sdk.integrations.celery import CeleryIntegration
from sentry_sdk.integrations.redis import RedisIntegration

from .base import *
import django_heroku
import sentry_sdk
from sentry_sdk.integrations.django import DjangoIntegration

Build paths inside the project like this: os.path.join(BASE_DIR, ...)

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(file)))

SECURITY WARNING: don't run with debug turned on in production!

DEBUG = config('DEBUG', '', cast=bool)

Application definition

Database

docs.djangoproject.com/en/2.2/ref/...

DATABASES = {
'default': {
'ENGINE': 'django.contrib.gis.db.backends.postgis',
}
}

------------------- HEROKU----------------------

SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')
SECURE_SSL_REDIRECT = config('SECURE_SSL_REDIRECT', default=False)
django_heroku.settings(locals())

--------------------------SENDGRID-----------------------

EMAIL_BACKEND = "sendgrid_backend.SendgridBackend"
SENDGRID_API_KEY = config('SENDGRID_KEY', '')
SENDGRID_SANDBOX_MODE_IN_DEBUG = False
SENDGRID_TRACK_CLICKS_HTML = False
SENDGRID_TRACK_CLICKS_PLAIN = False
DEFAULT_FROM_EMAIL = 'Support Team support@intelconstruct.com'

----------------- SENTRY-------------------------

if not DEBUG:
sentry_sdk.init(
dsn="11add338ccc345c6a402cd8cb20d2bc1@o...",
integrations=[DjangoIntegration(), CeleryIntegration(), RedisIntegration()],

    # If you wish to associate users to errors (assuming you are using
    # django.contrib.auth) you may enable sending PII data.
    send_default_pii=True
)
Enter fullscreen mode Exit fullscreen mode

django_heroku.settings(locals())
SIMPLE_JWT = {
"ACCESS_TOKEN_LIFETIME": timedelta(weeks=521), # 10 years
"REFRESH_TOKEN_LIFETIME": timedelta(weeks=521),
}

Your stuff...

------------------------------------------------------------------------------

REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': (
'rest_framework_simplejwt.authentication.JWTAuthentication',
),
# Permission settings
'DEFAULT_PERMISSION_CLASSES': [
'rest_framework.permissions.IsAuthenticated'
],
}
DEFAULT_AUTO_FIELD = 'django.db.models.AutoField'
DJSTRIPE_USE_NATIVE_JSONFIELD = True

these are required by Herokuuu

GEOS_LIBRARY_PATH = '/app/.heroku/vendor/lib/libgeos_c.so' if os.environ.get('ENV') == 'HEROKU' else os.getenv('GEOS_LIBRARY_PATH')
GDAL_LIBRARY_PATH = '/app/.heroku/vendor/lib/libgdal.so' if os.environ.get('ENV') == 'HEROKU' else os.getenv('GDAL_LIBRARY_PATH')

Thread Thread
 
sirneij profile image
John Owolabi Idogun

Your configurations don't conform with some of the details of this post. If you follow through, the following is not needed:

...
GEOS_LIBRARY_PATH = '/app/.heroku/vendor/lib/libgeos_c.so' if os.environ.get('ENV') == 'HEROKU' else os.getenv('GEOS_LIBRARY_PATH')
GDAL_LIBRARY_PATH = '/app/.heroku/vendor/lib/libgdal.so' if os.environ.get('ENV') == 'HEROKU' else os.getenv('GDAL_LIBRARY_PATH')
Enter fullscreen mode Exit fullscreen mode

They are the prime suspects of your issues.

Thread Thread
 
mojemoron profile image
Micheal Ojemoron

I added that when i discovered the issue, removing it still reproduces the issue. I am on Django 3.2.1 and Heroku 20

Thread Thread
 
sirneij profile image
John Owolabi Idogun

Did you add any buildpack aside the default one heroku gives?

Thread Thread
 
mojemoron profile image
Micheal Ojemoron

no

Thread Thread
 
sirneij profile image
John Owolabi Idogun

GDAL can't work on heroku without you adding external buildpack to help you build Geospatial configurations. That is why I recommend you go through this article again and implement the stuff suggested. They are easy and not detrimental.

Thread Thread
 
sirneij profile image
John Owolabi Idogun • Edited

The error you are getting sterms from directing heroku to look for buildpacks and runtimes in arbitary paths that do not really exist.

Thread Thread
 
mojemoron profile image
Micheal Ojemoron

i added this heroku buildpacks:add --index 1 github.com/heroku/heroku-geo-build...
according to your article

Thread Thread
 
sirneij profile image
John Owolabi Idogun

In your settings.py, add this to the bottom of the file:

DATABASES['default']['ENGINE'] = 'django.contrib.gis.db.backends.postgis'
Enter fullscreen mode Exit fullscreen mode
Thread Thread
 
mojemoron profile image
Micheal Ojemoron

in my settings, i already added Database Engine and the error i am getting is related to the library gdal

Thread Thread
 
sirneij profile image
John Owolabi Idogun • Edited

What database are you using? And do you use heroku's dj_database_url?

Collapse
 
ecngjohn10011 profile image
Daniel John

Thank you very much. Followed the instructions and things went smoothly.