Skip to content

pspadale/deploydjangoheroku

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Deploy Django Project to Heroku

  1. Create Heroku Account : https://www.heroku.com/
  2. Download and Install Git : https://git-scm.com/downloads
  3. Download and Install Heroku CLI : https://devcenter.heroku.com/articles/heroku-cli#download-and-install
  4. Open Terminal
  5. Login into Heroku CLI. Run below command it will open Browser then Click on Login
      heroku login 
  6. Create Repo for Project
      git init
  7. Add All Files to Repo
      git add . 
  8. Commit All Changes
      git commit -m "any comment"
  9. Create an App using Dashboard or Shell (I am creating using Shell)
      heroku create heroku_app_name
  10. Set Repo
     heroku git:remote -a heroku_app_name
  11. Install gunicorn or waitress - This will be our production server as we can not use development server which we were using by runing python manage.py runserver. Waitress is meant to be a production-quality pure-Python WSGI server with very acceptable performance. For More: https://docs.pylonsproject.org/projects/waitress/en/latest/
     pip install waitress
  12. Run wsgi.py file using waitress to test everything works fine on Local Machine (Before Pushing to Heroku)
     waitress-serve --port=8000 inner_project_folder_name.wsgi:application
  13. You will get a link in terminal just open it. If everything works then you will be able to see your project running on Web Browser
  14. If you get an error: Disallowed Host Invalid HTTP_HOST header then do below change in Django's Settings.py file and re-run wsgi.py file as Step 12
     ALLOWED_HOSTS = ['*']
  15. Create a file named Procfile then write below code in the file
      web: waitress-serve --port=8000 inner_project_folder_name.wsgi:application

if this don't work then try this one: javascript web: waitress-serve --listen=127.0.0.1:8000 webapp.wsgi:application

  1. Run below command - This will use Procfile to run the project. You will see an URL open it if everything file you will see project in browser

      heroku local
  2. Now go to your Django project's settings and do below change

      DEBUG = False
      ALLOWED_HOSTS = ['heroku_app_name.herokuapp.com', 'localhost']

    There is sometimes a error relating to CSRF, then try this one as well

      CSRF_TRUSTED_ORIGINS = ['https://heroku_app_name.herokuapp.com']

    As you have created an Heroku App so you have your app url e.g. https://heroku_app_name.herokuapp.com/ You can find it follwoing Heroku's Dashboard -> Setting

  3. We will also create Config Var for Django Project's Secret Key by following

    1. Copy SECRET_KEY from Django's settings.py File
    2. Go to Heroku App Setting then click Reveal Config vars then write
          SECRET_KEY r6t3d0udsdsdew5656+u9d+%o#^uo0su-i3x3_5zs5-5r7r9a1_mhwfi!2b+^
    3. Click Add
    4. Go to Django Project Settings.py and do below changes
          import os
          SECRET_KEY = os.environ['SECRET_KEY']
  4. If you have static files must include STATIC_ROOT in Django's settings.py file

      STATIC_ROOT = BASE_DIR / "static"
  5. Install whitenoise - WhiteNoise allows your web app to serve its own static files, making it a self-contained unit that can be deployed anywhere without relying on nginx, Amazon S3 or any other external service. (Especially useful on Heroku, OpenShift and other PaaS providers.) For More: http://whitenoise.evans.io/en/stable/

      pip install whitenoise
  6. Open Django's settings.py file and Add Whitenoise Middleware

      MIDDLEWARE = [
        # 'django.middleware.security.SecurityMiddleware',
        'whitenoise.middleware.WhiteNoiseMiddleware',
        # ...
      ]
  7. Bundle all requirements

      pip freeze > requirements.txt
  8. Make sure you have changed web: waitress-serve --port=8000 inner_project_folder_name.wsgi:application to web: waitress-serve --port=$PORT inner_project_folder_name.wsgi:application in Procfile before pushing to heroku

if this don't work then try this one: javascript web: waitress-serve --listen=*:$PORT webapp.wsgi:application

  1. Run below command
      git add .
      git commit -m "any comment"
      git push heroku master
  2. Done

Common Error while Deploying

Error 1: django-assets rejected
Cause: You haven't specified STATIC_ROOT
Solution: Either Provide STATIC_ROOT or Disable it by running heroku config:set DISABLE_COLLECTSTATIC=1

Error 2: No web processes running
Cause: Unable to find wsgi or havent configured Procfile
Solution: Config Properly Procfile

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Python 91.3%
  • HTML 7.0%
  • Other 1.7%