Deploying Django Apps in Heroku
Heroku is a platform as a service (PaaS) that enables developers to build, run, and operate applications entirely in the cloud.If you have a simple Django app that doesn't require huge storage or database, Heroku is the best choice. Their free service will suffice your need. Your app will be available almost 24/7 (~23 days up time in a month). You are given 550 hours per month and if you have a credit card, an additional of 450 hours will be rewarded. Enough to make your app to be available always. The server also sleeps after 30 mins of inactivity. This enables you to save uptime hours.
In deploying Django in Heroku you just need to have this ff. files:
- .gitignore
- runtime.txt
- requirements.txt
- Procfile
- Django App
.gitignore is a text file containing the type of files within the directories that wont be included in the deployment of app
runtime.txt is a text file containing the version of Python that the app will use. Example: python-2.7.14
requirements.txt is a text file that can be generated using pip freeze command. It contains all the modules needed by the app to run.
Procfile is a file containing the command that will be used for your app to run in Heroku. Example. web: gunicorn rainfallmonitoring.wsgi --log-file -
rainfallmonitoring in the example is the name of the Django Project, change it with yours.
Basically, you requirements.txt file should have:
- whitenoise
- gunicorn==19.1.1
A few line of code should also be added to the settings.py:
MIDDLEWARE = [
'whitenoise.middleware.WhiteNoiseMiddleware',
...
...
]
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.11/howto/static-files/
STATIC_ROOT = os.path.join(PROJECT_ROOT, 'staticfiles')
STATIC_URL = '/static/'
# Extra places for collectstatic to find static files.
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'rainfallmonitoringapp/static'),
)
change rainfallmonitoringapp with your app name, you can read more about it here.
Note:
Enabling gzip functionality using whitenoise gives error in deployment esp. when DEBUG=False.I choose Dropbox as a Deployment Method. If I want to update the app, I just overwrite the files and deploy the changes after. Don't worry, Heroku has version control and so with the Dropbox.