In this post, We'll be settings up a Django project to deploy on Heroku. Heroku is an almost free platform for users to deploy their python, node js, and other framework-based applications.
1. First, I would recommend making a copy of your project or use a separate git branch.
2. Make sure the python virtual environment is activated.
3. You have to create a text file called
requirements.txt at the root of the project to add all dependencies with their versions.
So to do that, you can manually type the dependency list and the versions you used for your Django project.
freeze command on your terminal at the project root directory
pip freeze > requirements.txt
Sometimes, if you have installed huge python libraries on your computer, this might be a change. That means the
requirements.txt file will have every library that you installed on the computer. Also, mostly the Django project doesn't need those other libraries. So there's a way to fix this problem that I would recommend. You have to install a simple library called pipreqs.
pip install pipreqs
Okay, then open your terminal and run this command.
After the process, you can see it will output.
Successfully saved requirements file in <your-project-location>/requirements.txt
4. Add this code line in the
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
5. Next, if you don't have a Heroku account, create a new Heroku account.
6. Download and install Heroku CLI.
7. In this step, you have to configure Django-Heroku.
Make a file called
Procfile at the root of the project. (do not add any file extension like .txt .py). The file name must be only
This file is used to explicitly declare your application’s process types and entry points. It is located at the root of your project.
Write code line inside
web: gunicorn <your-project-name>.wsgi
For more details, refer django-heroku documentation.
Okay, then you have to install two more libraries. This
Gunicorn, the production web server that Heroku recommend for Django applications.
pip install gunicorn
pip install django-heroku
settings.py import django_heroku top of the file.
Also, add this code line bottom of the
settings.py file to activate django-heroku.
Then, you have to add those
django_heroku libraries in the
requirements.txt file. In my case, the
(You can get those two dependencies versions by running the
pip list command.)
8. Finally, run those commands one by one in the terminal on the project root directory.
git init git add . git commit -m "first commit" heroku login heroku create <your-app-name> git push heroku master heroku open
You can see your project will open after the successful deployment. But if you used SQLite database on the project, it will show the missing database error . Run database migration to fix the issue.
heroku run python manage.py migrate
Now your project is live on Heroku🎉
If this not working or getting some other errors, Close your terminal and re-open it. Then change these code lines in the
DEBUG = False ALLOWED_HOSTS = ['<your-app-name>.herokuapp.com', 'localhost', '127.0.0.1']
If you made those edits, then run this commands in the terminal
git add . git commit -m "settingspy edited" git push heroku master
Hope it will work!👏🎉