Deploy Django web application

xarala221

Ousseynou Diop

Posted on December 7, 2019

Deploy Django web application

Deploy Django web application to Heroku

Introduction

Heroku is a cloud application platform, it facilitates the deployment of a web application.
They support several programming languages, include Python.

Install Heroku Toolbet

To install Heroku Toolbet Sign up to heroku,
Then install the Heroku Toolbet it is a very powerful command-line tool it will help you to manage your application.
After installing the Toolbet, open your Terminal/CMD and login to your account :

$ heroku login
Enter your Heroku credentials.
Email: your email
Password (typing will be hidden):
Authentication successful.
Enter fullscreen mode Exit fullscreen mode

Preparing our application

place into your application

$ cd my-application/
Enter fullscreen mode Exit fullscreen mode

Here is the list of things you will probably need to add to your project:

  • Add a Procfile in the project root;
  • Add requirements.txt file with all the requirements in the project root;
  • Add Gunicorn to requirements.txt;
  • A runtime.txt to specify the correct Python version in the project root;
  • Configure whitenoise to serve static files.

The Procfile

  • Create a file named Procfile in the project root
  • Add the following content :

web: gunicorn my-application.wsgi --log-file -

Note: change my-applicatio with the name of your Django project.

The requirements.txt

Run this command, this command will list all dependencies :

$ pip freeze > requirements.txt
You should see somthing like this :
dj-database-url==version
Django==version
gunicorn==version
psycopg2==version
psycopg2-binary==version
pytz==version
whitenoise==version
Enter fullscreen mode Exit fullscreen mode

The runtime.txt

Create a file named runtime.txt in the project root, and put the specific Python version your project use:

python 3.8

Set Up The Static Assets

Configure the STATIC-related parameters on settings.py:


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/2.2/howto/static-files/
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATIC_URL = '/static/'
STATICFILES_DIRS = [
    os.path.join(BASE_DIR, "static")
]
Enter fullscreen mode Exit fullscreen mode

Configure Whitenoise

Install Whitenoise

$ pip install whitenoise
Enter fullscreen mode Exit fullscreen mode

Add the Whitenoise to your Django application in the wsgi.py file:

"""
WSGI config for repertoire project.
It exposes the WSGI callable as a module-level variable named ``application``.
For more information on this file, see
https://docs.djangoproject.com/en/2.2/howto/deployment/wsgi/
"""
import os
from whitenoise.django import DjangoWhiteNoise
from django.core.wsgi import get_wsgi_application
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'my-application.settings')
application = get_wsgi_application()
application = DjangoWhiteNoise(application)
Enter fullscreen mode Exit fullscreen mode

Update the settings.py

STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage'
Enter fullscreen mode Exit fullscreen mode

Add whitenoise middleware at the top of the middleware list in settings.py

Deploy the application

Update Database Configuration in settings.py (at the bottom of the file)

import dj_database_url 
prod_db  =  dj_database_url.config(conn_max_age=500)
DATABASES['default'].update(prod_db)
Enter fullscreen mode Exit fullscreen mode

Create App in Heroku from terminal

$ heroku create myapplication
Creating ⬢ myapplication... done
https://myapplication.herokuapp.com/ | https://git.heroku.com/myapplication.git
Enter fullscreen mode Exit fullscreen mode

Choose any name for your app. Heroku will inform you if the name already exists

Add your app domain name to ALLOWED_HOSTS in settings.py.

ALLOWED_HOSTS = ['myapplication.herokuapp.com']
Enter fullscreen mode Exit fullscreen mode

Initialize Git and connect your new app (or existing one) to Heroku Git remote repository and push the application.
in your terminal / CMD :

$ git init
$ heroku git:remote -a myapplication
$ git add .
$ commit -m "Initial commit"
$ git push heroku master
Enter fullscreen mode Exit fullscreen mode

If you get an error message with collectstatic, simply disable it by instructing Heroku to ignore running the manage.py collecstatic command during the deployment process.

$ heroku config:set     DISABLE_COLLECTSTATIC=1  
Enter fullscreen mode Exit fullscreen mode

Then, run :

$ git push heroku master
Enter fullscreen mode Exit fullscreen mode

Migrate the database :

$ heroku run python manage.py migrate
Enter fullscreen mode Exit fullscreen mode

Let me know in the comment section if you've any error.

Thank you for reading.

💖 💪 🙅 🚩
xarala221
Ousseynou Diop

Posted on December 7, 2019

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Deploy Django web application
django Deploy Django web application

December 7, 2019