Deploy your django Project

fayomihorace

Horace FAYOMI

Posted on December 31, 2022

Deploy your django Project

This is the fifth and last part of a tutorial course in which you'll learn and understand the basics of Django by building a clone of the popular website Netflix.

Django and Netflix

In this tutorial, you'll learn how to deploy a Django application easily using fly.io.

Important note: Even though fly.io has a free version, they need your credit card to be set.

1- In the /django_netflix_clone/settings.py file set the ALLOWED_HOSTS configuration to accept all hosts.

# django_netflix_clone/settings.py
# Add this line
ALLOWED_HOSTS = ["*"]  # new
Enter fullscreen mode Exit fullscreen mode

2- Install Gunicorn as our production server.

python -m pip install gunicorn
Enter fullscreen mode Exit fullscreen mode

3- Update the requirements.txt file to list all the packages in the current Python virtual environment.

pip freeze > requirements.txt
Enter fullscreen mode Exit fullscreen mode

The new content of the requirements.txt should now be similar to:

asgiref==3.5.0
backports.zoneinfo==0.2.1
Django==4.0.2
gunicorn==20.1.0
Pillow==9.0.1
sqlparse==0.4.2
Enter fullscreen mode Exit fullscreen mode

Replace backports.zoneinfo==0.2.1 with backports.zoneinfo;python_version<"3.9" if your python version is greater than or equal to 3.9.

4- Install flyctl (the command line utility to deploy on fly.io)

Follow the instructions on the website https://fly.io/docs/hands-on/install-flyctl/ depending on your OS.
For Linux we should run:

curl -L https://fly.io/install.sh | sh
Enter fullscreen mode Exit fullscreen mode

then run

export FLYCTL_INSTALL="/home/horacefayomi/.fly"
export PATH="$FLYCTL_INSTALL/bin:$PATH"
Enter fullscreen mode Exit fullscreen mode

to add flyctl to the path.

5- Create an account on fly.io (skip if you already have one)

Run

flyctl auth signup
Enter fullscreen mode Exit fullscreen mode

Your browser will open up with the Fly sign-up screen.

6- Login to fly.io

Run

flyctl auth login
Enter fullscreen mode Exit fullscreen mode

Your browser will open up with the Fly sign-in screen.

7- Configure the application

To configure and launch the app, run the fly launch command and follow the wizard. You can set a name for the app and choose a default region. You can also choose to launch and attach a Postgresql database and/or a Redis database though we are not using either in this example.

To do that run:

fly launch
Enter fullscreen mode Exit fullscreen mode

Important:

  • Make sure the project name is the same as Django project name. But in our case, the name is django_netflix_clone which contains underscore _ which is not allowed by fly.io. So we leave the project name blank and wait the command execution to finish. A DockerFile will be generated and will modify it. At the end of the file, replace "demo.wsgi" by "the_project_name_with_non_allowed_underscore.wsgi"(in our case "django_netflix_clone.wsgi"
  • Accept by taping y when the prompt asks for database:
? Would you like to set up a Postgresql database now? (y/N)
Enter fullscreen mode Exit fullscreen mode
  • Add the following line into the generated DockerFile:
...
RUN python manage.py collectstatic --noinput 
# Add the two lines below here
RUN python manage.py migrate --noinput
RUN python manage.py loaddata netflix/fixtures/initial.json
...
Enter fullscreen mode Exit fullscreen mode

8- Deploy your app

Run

fly deploy
Enter fullscreen mode Exit fullscreen mode

If everything goes well, you should a similar output:

Image description
With a link you can click to open the monitoring page:

Image description

9- Open your application

Run

fly open
Enter fullscreen mode Exit fullscreen mode

There we go, your website should be available online now.
Here is the link to the fly.io version of the tutorial:
https://morning-tree-7095.fly.dev/

Image description

Congratulations again. 🎉.
We are at the end of this course.
You're a champion for making it to the end.

To conclude this adventure, I want to say first thank you for choosing this tutorial.
Second, this tutorial just scratches the surface of all Django is capable of. There is much other stuff to learn. So feel free to check out other content to consolidate and become a better Django developer.
Thank you again.
Sincerely, Horace FAYOMI 😊.

💖 💪 🙅 🚩
fayomihorace
Horace FAYOMI

Posted on December 31, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

What was your win this week?
weeklyretro What was your win this week?

November 29, 2024

Where GitOps Meets ClickOps
devops Where GitOps Meets ClickOps

November 29, 2024

How to Use KitOps with MLflow
beginners How to Use KitOps with MLflow

November 29, 2024

Modern C++ for LeetCode 🧑‍💻🚀
leetcode Modern C++ for LeetCode 🧑‍💻🚀

November 29, 2024