Contact US


Get Your Project Done by our experts
info@fundaofwebit.com

Deploy your django app on Heroku for free

Whatsapp

https://fundaofwebit.com/post/how-to-deploy-django-app-on-heroku

Deploy your django app on Heroku

To host your django app, first you have to install few pakages as shown below:

pip install gunicorn

once gunicorn is installed, install the whitenoise dependency using the following command.

pip install whitenoise

Now, create a file with the name as "Procfile" and do not add any extension to it. Open the file and paste the below code:

web: gunicorn projectname.wsgi --log-file -

 For Example, our project name is blog. So the code in procfile will be as shown below :

web: gunicorn blog.wsgi --log-file -

Now, we need to add a requirements.txt file. All the required dependencies will be mentioned in that file for Heroku to know the requirements of our project. To create this file, just paste the following command in your gitbash/ command prompt.

pip freeze > requirements.txt

You can open the requirements.txt file and you will find all the requirements and dependencies required to run your django app.

Open the settings.py file and in the ALLOWED_HOSTS just add the domain name/appname which you have created in heroku. This will allow that domain to access our project. But your project will not run on the localhost now. To run it on the localhost, just add the localhost in the list as shown below.

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

For Example, our heroku appname is blogom, when you click the view app in heroku, it will open a new tab in the browser with the address blogom.herokuapp.com. Mention that domain name in th eallowed host list in the  :

ALLOWED_HOSTS = ['localhost','blogom.herokuapp.com']

You can use either 127.0.0.1 or you can just write "localhost" to run your django app on the localhost.

 Note: When deploying your django app, set the debug to false.

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = False

Add the whitenoisemiddleware after the SecurityMiddleware and before the SessionMiddleware as shown below:

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',
]


When deploying to heroku, you will get an error that heroku can't get staticfiles. To remove that error, just create a folder in your main directory and name it staticfiles. Open your settings.py file and add the below code :

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.0/howto/static-files/

STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles'#add this line here

STATIC_URL = '/static/'

STATICFILES_DIRS = [
    os.path.join(BASE_DIR, 'static'),
]

And now our django app ready to deploy. Watch the above video for step by step demonstration.

Tags: How to Deploy django app on Heroku, Host django app on heroku, Deploy django app for free on Heroku

© Copyright - 2018 - 2021 | All rights reserved at Funda of Web IT