How to put Django on heroku server in 2020. 10 steps

I decided to share with you how to put a project written in Python / Django on the heroku server. Heroku is a free hosting service for testing your projects. If you need to see how the project works in combat mode - go ahead!



1. You need to register on heroku. There is nothing complicated in this, just enter the data, confirm your account by mail, and go ahead.



2. Installing the command line heroku. , at the top left we see the burger menu, click on it and select - Documentation -> Python , click Get Start With Python . Next, on the left, click Set Up and select the heroku console installation on your operating system, there is nothing complicated, just install it as you like and that's it.



3. Close the browser for now and go to the command line or bash . Go to the folder with our django project and open the project in a text editor (in my case Pycharm ). Next, we will have to work with the git version control system. If you do not have this utility, you can download it at git-scm.com/downloads . Go through the easy installation and return to this article.



4. In our console, write the command:



git init


Then we create a .gitignore file in the project directory . In it, we can write all the files that we want to ignore when uploading to the server. Let's say I will use the MySQL database on the server, so I don't need the db.sqlite3 file .



We write this code:



__pychache__/
*.pyc
db.sqlite3


Then we write 3 commands to bush




git add .
git commit -m "GIT init"


1st is responsible for adding all the files to git.



2nd for saving these files on the computer locally with the message GIT init.



5. Now we enter our heroku through the console. We write:



heroku login


Next, first enter E-mail, press Enter. Then the password and again Enter.



So we entered. Next, we create an application, we will do this through the console, so we enter the following into it:



heroku create


The team builds the application. After this command, you can write the name of the application separated by a space. Otherwise heroku will generate it automatically and output it to the console.



6. Next, create several files so that heroku understands what we are loading and how:



Procfile

runtime.txt



  1. At runtime , we immediately write this code:



    python-3.8.5
    


    After python-, write your python version.

  2. Procfile :



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


    Instead appname write the name of your project.





Next, install gunicorn itself to serve django via wsgi :



pip install gunicorn


Immediately install whitenoise to work with static files:



pip install witenoise


7. Now go to settings.py and make the following changes:



ALLOWED_HOSTS = ['*']


Add static_root if you don't have it:



import os

STATIC_ROOT = os.path.join(BASE_DIR, 'static')


8. Setting up work with the database. Install the utility for more convenient work:



pip install dj-database-url


go back to the settings and write:



import dj-database-url

db_from_env = dj-database-url.config()
DATABASE['default'].update(db_from_env)


9. The last file we need is requirements.txt , it will contain all installed libraries:



pip freeze -> requirements.txt


We have created a file with all packages. You can write various packages with their versions to it. It is imperative to write this line:



psycopg2==2.8.6


If you encounter errors during further actions, see if you need to add a package here.



10. Well, the final, upload it to the server.



Go to the console and write the following commands:




git add .
git commit -m "Diploy"
git push heroku main


If you fail with main , try:



git push heroku master


And the process of uploading our project to heroku started. Then a link to our project will be written to your console. We will go through it later, and now we will perform all the migrations :



heroku run python manage.py migrate


And create a super user :



heroku run python manage.py createsuperuser


We follow the link received earlier, and we see our project. This is how in 10 steps we uploaded our project to heroku and set up the database. Thank you all for your attention.



All Articles