Site icon Coding is Love

Step by step guide to deloy Django Applications on Heroku

Introduction

Most of the software development happens on localhost. But this locally hosted software can only be viewed and tested by one developer.  In this world of worldwide collaboration and continuous integration, localhost does not suffice. So, we deploy the test or dev version of the software on a cloud so that all the team members can see it and test it.

So in this post, we are going to see how a Django project can be hosted on Heroku to be viewed by the entire world. In our case, we are going to use the GitHub integration method to deploy code to Heroku because it is the easiest.

What is Django?

Django is a web framework built for Python, generally used for rapid development of websites or web apps. Django has a clean and pragmatic design. They say that Django is built for entrepreneurs. Because it takes care of most of the tedious requirements of web development. This lets you focus solely on developing the app/site.

Django is open source and encourages new developers to contribute to it. As a result of this, it has a wide range of plugins and packages available. The official development repository of Django is hosted on GitHub.

What is Heroku?

In my previous post, we talked about BaaS. This post is about Heroku, which is a PaaS (or Platform as a Service). In a PaaS, developers are provided with a cloud-based platform on which they can run their apps or websites for the world to see. Heroku is the most popular free PaaS service. It supports most of the languages and frameworks.

Dependencies and Prerequisites:

pip install django gunicorn dj-database-url whitenoise psycopg2

Get Django ready for Heroku:

  1. Start a Django project (skip if you already have one)
    django-admin.py startproject MyProjectName
  2. Go into the MyProjectName/ directory and execute the command
    python manage.py migrate
  3. Create a file called “Procfile” (no extension)  with the following contents:
     web: gunicorn MyProjectName.wsgi --log-file -
  4. Create a file called “requirements.txt” by executing:
    pip freeze > requirements.txt
  5. Paste the following code at the end of the settings.py file inside MyProjectName directory in your project
    import dj_database_url
    
    db_from_env = dj_database_url.config(conn_max_age=500)
    DATABASES['default'].update(db_from_env)
    
    PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__))
    
    STATIC_ROOT = os.path.join(PROJECT_ROOT, 'staticfiles')
    
    STATICFILES_DIRS = (
    os.path.join(PROJECT_ROOT, 'static'),
    )
    
    STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage'
  6. Make a folder called “static” inside the inner MyProjectName directory.
  7. Added “‘myprojectname.herokuapp.com'” to the ALLOWED_HOSTS list in the settings.py file.

Pushing the code to Github:

  1. After registering/logging in on Github, create a new repository
  2. Install the GIT client for your computer if you haven’t already. If you don’t know how to, then this will help.
  3. Push the code to the newly created repository via the GIT client. Follow the below GIF for assistance.

Integrating Heroku with GitHub:

  1. Register/Login to Heroku
  2. Go to the dashboard and create a new app
  3. Go to the deploy tab and link your GitHub account and the repository
  4. Scroll down to the end of the page and click on “Enable Automatic Deploy” and then “Deploy Branch”

Now your Django project should be up and running on Heroku. Hope this tutorial helped you. If you run into any problems, reach out in the comments below.

Resources:

 

 

Exit mobile version