Hey, Web Developers 👨💻!
While you were working on web development, you must have come across certain issues. So, in this article, we’ll be covering those troublesome issues and figuring out how to deploy Django on Heroku.
Some issues appear during your web development task so here I will be covering up a very frustrating problem which is How To Deploy Django On Heroku.
Before we leap into the guide of deployment of Django on Heroku, let’s understand the need for deploying Django on Heroku.
By deploying a Django application using Heroku in python, The developers can use Django from anywhere using the free service of Heroku.
Deploying Django on Heroku can be quite a stressful task with the prohibition of static files, bugs, and glitches that you may wanna or may not wanna encounter.
Just stick to the article and we’ll figure out the easiest way possible to Deploy Django On Heroku.
Let’s start with What Is Django?
Django is the most popular open-source framework in the web development industry.
It is a high-level python web framework specially built to boost up the development of secure and maintainable websites. Built by experienced developers.
Django follows the MTV(model-template-view) architecture pattern, it takes care of the Hassle of web development issues, so that you don’t have to be worried about the foundation of the code. As it has pre-installed functions and classes, You have to maintain your focus on writing your app.
Do you know What Is Heroku?
Heroku is a cloud platform service that supports several famous programming languages.
Heroku is one of the most known Cloud platforms also known as the PaaS(platform as a service). It supports various programming languages including python and totally used by developers to deploy, run and scale applications in the simplest path to upload their apps in the market. Heroku also allows developers to deploy any 5 web applications under the size of 500 MB.
Okay! This was just the intro part of Django and Heroku and keeping time in mind we don’t need to dig into the details.
Now, let’s go through the process of deploying a python app and Django on Heroku, Also Check Django deployment on shared hosting.
What We are going to Learn in this Tutorial?
Expected files for Python
If any of these three files are present in Heroku’s root directory. It will automatically identify your app as a python app.
If not then the python buildpack will fail to identify your application correctly.
How To Install Heroku Toolbelt
Run the below command on your cmd.
$heroku version
If cmd prints the version of Heroku that means it is installed.
$heroku login
Python deployment flow
When you deploy your python app to Heroku, the dependencies which you specify in your requirements.txt file automatically install before the app starts up.
If you are using Django’s collectstatic command which runs automatically during the deployment procedure. This command can be quite tricky to be configured properly. To make it a bit easier, add the Django-Heroku Python package which sets up everything.
To automatically perform other tasks before your app is deployed, you can add a release phase command to your app.
Python versions and upgrades
Generally, newly created python apps work on python-3.9.5 runtime and your app basically works on the version which you have used when it was first deployed.
You can specify a Python runtime, by adding a runtime.txt file to your app’s root directory that declares the exact version number to use.
If the app python version is not updated then you will be notified in your app’s build output.
Preparing the Application
So, The list of things which you’ll probably need for preparing the application project are:
The Procfile
Procfile explicitly declares that what command should be executed to start your app.
In the project root create a file named Proctfile and add the following code:
web: gunicorn <project_name>.wsgi --log-file -
The Requirements.txt
Create the main requirements.txt file in the root directory and Simply add this line of code:
pip freeze > requirements.txt
pip freeze command is used to show all the install files.
Whether your python application is deployed or not on Heroku. It will depend on the requirements.txt file. If it has a requirements.txt file in its root directory then Heroku will indicate the deployment of your python application.
Your requirements.txt file must be like this:
Django==1.9.8dj-static==0.0.6gunicorn==19.6.0Unipath==1.0python-decouple==3Pillow==3.3.0
The runtime.txt
Again, in the root directory add the runtime.txt file and specify the correct python version for your web app by adding the following line to your application
python-2.7.12
Set Up The Static Assets
Now, Open setting.py file and make these changes:
"INSTALLED_APP" list.INSTALLED_APPS = [ 'whitenoise.runserver_nostatic','django.contrib.staticfiles', # …
MIDDLEWARE = [ 'django.middleware.security.SecurityMiddleware', 'whitenoise.middleware.WhiteNoiseMiddleware', # ...]
WHITENOISE_USE_FINDERS = True
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
Deployment
The Heroku deployment process is done by using Git. Where your app will be stored in a remote git repository in the Heroku cloud.
Why do we need a Git repository?
Well, it acts as an update to your deployed app. Git repository even helps to restore app files when a new update is crashed or has too many annoying bugs. .git/ repository tracks down the changes made to a file in your project while maintaining the project history.
Alright, then it is time for the deployment.
Heroku login
heroku create demo-yourproject
Choose any name for your app. If the name already exists then Heroku will choose a name for you.
Output:
Creating ⬢ demo yourproject… donehttp://demo yourproject.herokuapp.com/ | https://git.heroku.com/demo yourproject.git
ALLOWED_HOSTS = ['herokudjangoapp.herokuapp.com']
Heroku addons:create Heroku-PostgreSQL:hobby-dev
Output : Creating PostgreSQL-metric-21979... done, (free)Adding PostgreSQL-metric-21979 to demo-Bootcamp... doneSetting DATABASE_URL and restarting demo-Bootcamp... done, v4The database has been created and is available
So, open setting.py and make these changes.
SECRET_KEY = config('SECRET_KEY')DEBUG = config('DEBUG', default=False, cast=bool)DATABASES = { 'default': dj_database_url.config( default=config('DATABASE_URL') )}
git push Heroku master
OutputCounting objects: 1999, done.Delta compression using up to 4 threads.Compressing objects: 100% (616/616), done.Writing objects: 100% (1999/1999), 461.28 KiB | 0 bytes/s, done.Total 1999 (delta 1139), reused 1983 (delta 1131)remote: Compressing source files... done.remote: Building source:remote:remote: -----> Python app detectedremote: -----> Installing python-2.7.12remote: $ pip install -r requirements.txt
remote: -----> Launching...remote: Released v5remote: https://demo-bootcamp.herokuapp.com/ deployed to Herokuremote:remote: Verifying deploy.... done.To https://git.heroku.com/demo-bootcamp.git * [new branch] master -> master
Heroku run python manage.py migrate
Output:Running python manage.py migrate on ⬢ demo-Bootcamp... up, run.9352Operations to perform: Apply all migrations: questions, sessions, authentication, articles, feeds, contenttypes, messenger, activities, authRunning migrations: Rendering model states... DONE Applying contenttypes.0001_initial... OK Applying auth.0001_initial... OK Applying questions.0001_initial... OK Applying feeds.0001_initial... OK Applying articles.0001_initial... OK Applying activities.0001_initial... OK Applying contenttypes.0002_remove_content_type_name... OK Applying auth.0002_alter_permission_name_max_length... OK Applying auth.0003_alter_user_email_max_length... OK Applying auth.0004_alter_user_username_opts... OK Applying auth.0005_alter_user_last_login_null... OK Applying auth.0006_require_contenttypes_0002... OK Applying auth.0007_alter_validators_add_error_messages... OK Applying authentication.0001_initial... OK Applying messenger.0001_initial... OK Applying sessions.0001_initial... OK
Now you’re good to go!
To check Visit yourproject.herokuapp.com
How to Deploy Django on Heroku: Step-by-step Guide