Django Everywhere-Host Your Django App for Free on Heroku

Learn how you can deploy your Django Application on the internet for free for no cost. A Simple and Easy service for hosting any 5 web app is provided by Heroku without any money. We will be using a Django App to showcase this service, hosting our application on Cloud.

How to Deploy a Django Application to Heroku - DEV
Django and Heroku

Heroku is a free cloud hosting service. Which can be used to deploy any 5 Web Apps app under the size of 500 MB to be deployed on the cloud. It also provides the service of a free Database (Relational or Otherwise) along with the server.

Free Hosting - host without cost
Free Hosting

Why Deploy A Django App during development?

Migrating a Django App from local server to a cloud opens us to an amazing learning and testing opportunity. I also allows to test our app on the internet and gauge its their performance. I also exposes the novice developer to using and learning the deployment configurations free of cost and in a stress free environment. Not to mention it is a perfect way for young developer to showcase their work to the world and to the non-technological employer they are often hired by.

Prerequisites:- A system loaded with any OS, internet connectivity, a test Django App for deployment, A GitHub Account, A Heroku account.

Step 1. Prepare the Django App

The Django App has to be prepared with a few configuration files before we can deploy it on the web.

how to create Heroku procfile for windows? - Stack Overflow
procfile

The first thing we have to do is make a procfile in our project which will be detected by Heroku on deployment. This will redirect the control to wsgi.py in the main app which by default handles the deployment work in Python/Django framework.

The Procfile will contain the following code.

web: gunicorn CCMS.wsgi --log-file -

Where your project name will replace the CCMS

Install gunicorn dependency

pip install gunicorn

Prepare a requirements.txt

This file in your project is used to tell the server what dependencies and their version that are required for the smooth deployment and working of your Web App.

pip freeze > requirements.txt

Update settings.py file

Set the following changes.

DEBUG = False

ALLOWED_HOSTS = ['127.0.0.1','sampledomain.com']

Original Middle ware Code

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

Updated Middle ware Code

Install whitenoise dependency

pip install whitenoise
MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'whitenoise.middleware.WhiteNoiseMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

Original urls.py

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include("Home.urls")),
]

Updated urls.py

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include("Home.urls")),

    url(r'^media/(?P<path>.*)$', serve,{'document_root':       settings.MEDIA_ROOT}), 
    url(r'^static/(?P<path>.*)$', serve,{'document_root': settings.STATIC_ROOT}), 
]
GitHub and Heroku

Step 2. Commit the project to GitHub

Once all these changes are in order commit them to any GitHub repository of your choice.

Step 3. Create and Setup a Heroku App

Heroku Dashboard
  1. By clicking on “New” you can open the Create New App.
  2. Choose an available domain name.
  3. Go to Settings and add a build pack of Heroku/python.
  4. Now switch to deploy panel.
  5. Select GitHub in the method of deployment(You may need to login to your GitHub account).
  6. Select appropriate branch.
  7. Click “Deploy” under “Manual Deploy”.
  8. Check your new website.
Django App Deployment on Heroku
Heroku Manual Deploy

Disclaimer:- You can check the logs to check what went wrong with deployment. In the log files.

One Comment

Leave a Reply

Your email address will not be published. Required fields are marked *