Episode #26 | Kode47 Django Template Part 2

Sunday, 28 Apr 2019 Kudzai Nyandoro 22:46 Minutes


In this episode we continue from where we left off, learn how to create a secret key, and configure our application in development and production. Finally, we deploy the Kode47 Django Template to Heroku.


  • Generate a new secret key and add it to the settings.py file or export to your .bash_profile.
$ python
>>> import secrets
>>> secrets.token_hex(24)
  • Add the following to your .bash_profile
# .bash_profile

export SECRET_KEY="4db736c4a169807620d3dcc0d2b5414dcd1bb5c5a100488b"
export DEBUG_VALUE="True"
  • Fire up the server
$ python manage.py runserver
  • In your browser visit localhost:8000

Add your application to version control

  • Rename the current git origin
$ git remote rename origin upstream

Push to Github

  • Visit https://github.com, log in or create and an account. Next, create your remote repository. If you're new to version control visit my blog and view episodes 8 and 9 for more on version control and how to set up a remote repository on Github.
$ git remote add origin https://github.com/kode47/animal_project.git
$ git push -u origin master

Or push to Gitlab

  • If you're using Gitlab for version control your can create your remote repository as shown below without leaving your console. I'm not sure how to do this on Github. You can also visit my blog and watch episode 22 for more on Gitlab. Be sure to replace kode47 with your username in the lines below. This is an optional way of creating your remote repository on GitLab.

  • Send your application to version control on GitLab.

$ git push --set-upstream git@gitlab.com:kode47/animal_project.git master
  • Add the origin
$ git remote add origin git@gitlab.com:kode47/animal_project.git
  • Change the project name to your own. In this example I'm using 'animal_project'
cd mv template_project animal_project
  • Open the manage.py file and update Line # 8
def main():
    os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'animal_project.settings') # Line 8
        from django.core.management import execute_from_command_line
    except ImportError as exc:
        raise ImportError(
            "Couldn't import Django. Are you sure it's installed and "
            "available on your PYTHONPATH environment variable? Did you "
            "forget to activate a virtual environment?"
        ) from exc

if __name__ == '__main__':
  • Open and update the Procfile
web: gunicorn animal_project.wsgi
  • Open and update Line 2 & 14 of the wsgi.py file
WSGI config for animal_project project. # Line 2

It exposes the WSGI callable as a module-level variable named ``application``.

For more information on this file, see

import os

from django.core.wsgi import get_wsgi_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'animal_project.settings') # Line 14

application = get_wsgi_application()

  • Open and update lines 2, , 25, 49 and 67 in the animal_project/settings.py file.
Django settings for animal_project project. # Line 2

ALLOWED_HOSTS = ['https://YourAppName.herokuapp.com'] # Line 25

ROOT_URLCONF = 'animal_project.urls' # Line 49

SGI_APPLICATION = 'animal_project.wsgi.application' # Line 67
$ git add -A
$ git commit -m "Rename application ready to deploy."
$ git push 
  • Create a Heroku app name
$ heroku create app_name
  • Configure Django on Heroku
$ heroku config:set SECRET_KEY="4db736c4a169807620d3dcc0d2b5414dcd1bb5c5a100488b"
$ heroku config:set DEBUG_VALUE="False"
  • Push to Heroku
$ git push heroku master
  • Inter into the Heroku shell
$ heroku run bash
  • Make migrations
$ python manage.py makemigrations
  • Run migrations
$ python  manage.py migrate
  • Create a superuser. This is optional.
$ python manage.py createsuperuser # Follow the prompts
  • Exit the Heroku shell
$ exit
  • Open your application in the browser
$ heroku open