Deploy your django Project
Published: 2022-12-31 18:42:07

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](https://www.netflix.com/).

![Django and Netflix](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g4qht1jfclf5qdz799mo.png)

In this tutorial, you'll learn how to deploy a Django application easily using [fly.io](https://fly.io/docs/django/getting-started/).

**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.
```python
# django_netflix_clone/settings.py
# Add this line
ALLOWED_HOSTS = ["*"]  # new
```

### 2- Install Gunicorn as our production server.
```shell
python -m pip install gunicorn
```

### 3- Update the `requirements.txt` file to list all the packages in the current Python virtual environment.
```shell
pip freeze > requirements.txt
```
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
```
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/](https://fly.io/docs/hands-on/install-flyctl/) depending on your OS.
For Linux we should run:

```shell
curl -L https://fly.io/install.sh | sh
```
then run
```shell
export FLYCTL_INSTALL="/home/horacefayomi/.fly"
export PATH="$FLYCTL_INSTALL/bin:$PATH"
```
to add `flyctl` to the path.

### 5- Create an account on fly.io (skip if you already have one)
Run
```shell
flyctl auth signup
```
Your browser will open up with the Fly sign-up screen.

### 6- Login to fly.io
Run
```shell
flyctl auth login
```
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:
```shell
fly launch
```
**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:
```shell
? Would you like to set up a Postgresql database now? (y/N)
```

- Add the following line  into the generated DockerFile:
```docker
...
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
...
```

### 8- Deploy your app
Run
```shell
fly deploy
```
If everything goes well, you should a similar output:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nvnpdwhr053ozrwq9pnf.png)
With a link you can click to open the monitoring page:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kkluyhz7yt80ytsdyi9k.png)

### 9- Open your application
Run
```shell
fly open
```
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/](https://morning-tree-7095.fly.dev/)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tgjk7f311zmxyjf3cvuj.png)

Congratulations again. :tada:.
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 :blush:.