After, two solid weeks of trying to move my Django project on to production, one thing I can tell you for a fact is that there is no sure fire way of deploying Django projects, but I can give you a 95% assurance that if you explicitly follow all the steps in this guide you’ll successfully deploy your project. Also, feel free to drop a comment if you encounter any challenge.
This article is in two parts, in the first phase I will show you how to deploy your Django projects on Heroku and in the second phase you’ll learn how to use cloud file storage services like aws s3 bucket for storing you media file uploads, I'll also be explaining all the git command used during this exercise incase you're entirely new to git, with that much said let’s dive in.
Step 1. You need to create a Heroku account Here and then create an app on Heroku.
Step 2. Download and install the following apps
git
andheroku cli
, if you already have these downloaded then skip this step and proceed to the next step.Step 3. Login into your Heroku account from terminal using this code
heroku login
Step 4. Now that you've successfully logged into account, you need to create a repository on your git and we will do that using the following command.
git init
- Initializes/create a repository/folder.
Next, type git status
- To check the current status of your git project if any changes have been made.
with git status
you can see all the changes that have been made on your project.
Step 5. You need Heroku to know where you want to push your project on its server by inputting the following line in your terminal
Heroku git: remote -a 'name-of-your-app-that-was-created-on-the-heroku-server'
Step 6. At this step we can choose to push but if we do you'll encounter some errors; some libraries were installed locally while you were creating your app and as such we need those libraries to also be installed on the heroku server or at least let heroku know those libraries exist in other to efficiently run your app on the Heroku server, now type the following code.
pip freeze > requirements.txt
Next, we need to install some libraries using pip, create a few files and edit our settings.py
file before we can push our project to the heroku server
pip install gunicorn
pip install whitenoise
Gunicorn: Acts as a middleman between your application and the web server’s application in GETing and POSTing requests
Whitenoise: whitenosie helps in rendering your static files on the Heroku hosting server.
- Step 7. After installing these libraries create a file in your root folder named
Procfile
and input the following line of code
web: gunicorn 'name-of-project.wsgi
NB. A Procfile does not have any file extension
Step 8. Run this code again
pip freeze > requirements.txt
to add your newly installed libraries to your requirements file.Step 9. Next, we need to make some adjustments to our
settings.py
file
Look for the line that has
ALLOWED_HOST = [ ]
and change it to
ALLOWED_HOST = ['*']
What this implies is that any domain address can easily access your django project once it’s been deployed, on the other hand if we specify an address say www.myproject.com this implies that you'll only be able to access this app only from this given domain.
Next, look for the MIDDLEWARE
section and add the following line of code there
whitenoise.middleware.WhiteNoiseMiddleware
NB: ensure it is added on the second line as you need your static files to be render just after the security libraries
Lastly, on your settings.py
file add the following lines of codes in your static section, so your static files can be properly rendered
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static')]
STATIC_ROOT =os.path.join(BASE_DIR, 'staticfiles')
- Step 10. Lastly we just need to run a few line of codes on our terminal and then finally push our code to the heroku server first run
python manage.py collectstatic
git status
git add .
git add .
includes all the changes we have made so far.
run git status
again and you'll notice our code has been added, next we need to commit our code to git with git commit -m 'First commit'
git commit
is giving git the sole right to make the changes to our codes and finally we push our code with.
git push heroku master
or
git push heroku main
VOILA!!!
Your project has been successfully deployed, you can view it by visiting the link usually in this format
'name-of-project'.herokuapp.com
NB: Many tutorials out there make use of the django_heroku or django_on_heroku, in 2022 if you try using these libraries I'm slightly sure you'll encounter an error I really don't have answers as to why but the only tangible answer I can give is that these libraries are no longer been maintained and as such are no longer compatible with some functionalities on the heroku server.
If you still experience any difficulty please do not hesitate to contact me.
Top comments (3)
Heroku is such a great platform for hosting applications. I would put it up there with Vercel and Netlify. Good guide btw.
Yah agree. but I think you can only deploy static projects on netlify last I checked tho.
JAMSTACK yes 😊