Skip to content

iamr0b0tx/deploy-django-to-heroku

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Deploy Django to Heroku

Deploy Django app to Heroku using this app. This is a step by step guide

Setup and Installation

Prerequisites

  • Python 3.6+ Download
  • Heroku Account Signup
  • A working django Project. If you don't have one you can clone this
  • virtualenv or an alternative

Steps

  1. Open your django project as a project in your Idea. I will be referring to pycharm in this tutorial. I will be referring to get-started-with-django Django project named mysite.
  2. Set up virtualenv. I will be referring to venv as the name of my environment. You can easily just add interpreter in pycharm.
    virtualenv venv
    venv\Scripts\activate # for windows users
    source venv\bin\activate # for linux users
    
  3. run the server to make sure the application works fine.
    python manage.py runserver 
    
  4. create a requirements.txt file. Add the contents of these file requirements.txt.
  5. create a runtime.txt file from runtime.txt ref{2-3}.
  6. install the requirements.
    pip install -r requirements.txt
    
  7. create a file named .env and add the contents in .env
  8. create a Procfile like Procfile the following at the top. ref{4}
  9. Add the following at the top. ref{4}
    import django_heroku
    from decouple import config
    
  10. Add the following at the bottom. ref{1,5}
    STATIC_ROOT = Path.joinpath(BASE_DIR, 'staticfiles')
    STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
    
    django_heroku.settings(locals())
    
  11. edit your project settings file and update the SECRET_KEY, DEBUG and ALLOWED_HOSTS as configured in settings.
    # SECURITY WARNING: keep the secret key used in production secret!
    SECRET_KEY = config('SECRET_KEY')
    
    # SECURITY WARNING: don't run with debug turned on in production!
    DEBUG = config('DEBUG', cast=bool)
    
    ALLOWED_HOSTS = []
    ALLOWED_HOSTS.extend(config('ALLOWED_HOSTS').split(','))
    
  12. Update the middlewares. ref{1,5}
    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',
    ]
    
  13. set up git branches. NB: Make sure you are using a repository that you set up so you can have push access.
    git add . 
    git commit -m "setting up main branch"
    git push
    
    git checkout -b dev main
    
    git add .
    git commit -m "setting up dev branch"
    git push origin dev
    
    git checkout -b indev dev
    
    git add .
    git commit -m "setting up indev branch"
    git push -u origin indev # this will set the remote to indev instead of main
    
  14. login to your heroku account. login
  15. Click on new to create new pipeline.
  16. Name the pipeline and connect the account to github and choose the repository
  17. Click add new app and create a new app in the staging
  18. Click add new app and create a new app in the production
  19. Click the app and visit resource tab and search for Heroku Postgres and apply the Hobby Dev Free.
  20. Visit the deploy tab and set teh automatic deploy to their respective branch
  21. Visit the settings tab and add same things in the .env in the config vars. The ALLOWED_HOSTS must be your heroku domain name.
  22. Make a Pull request to dev and master to see the update on heroku. Or just deploy directly on heroku.
  23. Visit the heroku console via the more button and run the following commands one after the other
    python manage.py migrate
    python manage.py createsuperuser
    

References

  1. whitenoise docs
  2. heroku python runtime
  3. heroku supported python versions
  4. django heroku
  5. django static assets

About

Deploy Django app to Heroku

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published