Sangeeta K

Jul 11, 20203 min

Deployment of Python Django Application on Heroku

Updated: Jul 14, 2020

Heroku is a platform to easily deploy and host your application,without going through the headache of setting up manually.Heroku has free tier where you can deploy your application without paying.

Heroku holds a user static files,but if you want to store static files like images you can use AWS S3 buckets as well.

Let’s go ahead and Signup account on Heroku by clicking below link. www.heroku.com

Pre-Requisites:

If Git Version Control is not installed,please use below link to install https://git-scm.com/downloads and choose Operating system you are using.

Once your Heroku account created,then install Heroku Command Line Interface(CLI). CLI allows to deploy the application from your Command Prompt or Pycharm Terminal by clicking the below link. You can install Heroku CLI and choose your Operating System .

https://devcenter.heroku.com/articles/heroku-cli#download-and-install

In order to check CLI is installed or not by checking in command prompt as mentioned below.

CMD>heroku

Commands to Deploy Application:

Lets login to Heroku account through command prompt.

By entering the below command, you will be prompted to Heroku login page by Pressing any key to open up the browser to login,then login to Heroku account and after you arelogged in through your command prompt.

CMD > Heroku login

Next you need to change the path to your project folder as mentioned below.

cd C:\PycharmProjects\YourProjectName

Let’s install some packages required for deployment by running below commands.

pip install gunicorn

pip install boto3

pip install Django-storage

pip install Django-Heroku

pip install psycopg2

To check dependencies , run the below command in CMD

pip freeze

Now let’s create a requirements.txt file,which holds all your dependencies required for the project and it will store your dependencies by giving a below command.

pip freeze > requirements.txt

Check in the project folder whether file is created or not.

Create a folder called .gitignore in the project directory. Then copy all the files from below link and paste into newly created folder (i.e. .gitignore)

https://github.com/github/gitignore/blob/master/Python.gitignore

We need two more files to create in the project folder, Procfile and runtime.txt. For Procfile don’t mention any extension (i.e. .txt, or .html)

In Procfile you need paste the below code

web: gunicorn yourrootfoldername.wsgi

In runtime.file need to mention Python version which you are using i.e. python-3.x.x

Next step is to create a Heroku App in Heroku account by clicking NEW tab. NEW->Create New App , then Type app name .

This creates a domain name for your app,you open you domain name by using below command and it automatically open URL in browser with default site.

cmd>Heroku open

Or by using command line you can create your appname.

cmd>heroku create myherokuappavailable

cmd>Heroku open

Settings.py

Below code need to be added in settings.py file.

import django_heroku
 

 
import psycopg2

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

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

import dj_database_url
 

 
db_from_env=dj_database_url.config(conn_max_age=600)
 

 
DATABASES['default'].update(db_from_env)

STATIC_ROOT = os.path.join(BASE_DIR,'staticfiles')

#If you are using email functionality and Postgress for database in RDS #code.

EMAIL_HOST_USER = os.environ.get('EMAIL_ID')
 

 
EMAIL_HOST_PASSWORD = os.environ.get('EMAIL_PASSWORD')

DATABASES = {
 

 
    'default': {
 

 
        'ENGINE': 'django.db.backends.postgresql',
 

 
        'NAME': 'postgres',
 

 
        'USER': os.environ.get('AWS_DB_USER'),
 

 
        'PASSWORD': os.environ.get('AWS_DB_PASSWORD'),
 

 
        'HOST': os.environ.get('AWS_HOST'),
 

 
        'PORT': '5432',
 

 
    }
 

 
}

Time to deploy your application to Heroku using Git commands:

To initiate your git repository,add,commit and push your code,please follow below commands.

git init

git add -A

To check file status

git status

git commit -m “Initial Commit”

git push heroku master

Now application is deployed to Heroku.

Let’s set all config environment variables through command prompt or add config variables from your Heroku app on website.

Run the below command on your command prompt to set config variable,for example:

cmd>heroku config:set email-id=”emailid@gmail.com”

above ‘email-id’ is your environment variable and ‘emailid@gmail.com’ is the value.By following the above command you can set your all config variables.

Or Set config variables on Heroku app website

myherokuappavailable->settings->Reveal config vars

If you change the code in project, repeat all above commands of Git to deploy the changes.

Command to migrate code

heroku run python manage.py migrate

Command to CreateSuperUser

heroku run python manage.py createsuperuser

Now you are all set,your application is deployed in Heroku server.

    770
    1