How to Deploy Django Website for Free in 2023
Tags: django, webdev, tutorial, programming
Published: 2023-03-09 06:21:08

You created your Django website and now want to see it in action online? Don't worry, this post will explain in detail that **How to Deploy Django Website for Free in 2023**. Although there are many websites available online to host django website for free, however this post will cover the website I use the most to deploy my django projects regularly.

## Introduction
Knowing how to deploy websites online is one of the crucial skill to learn during your web development journey. Deploying a Django Website is important as it allows users to access the website. Without deployment the website remains confined to the developer's local machine and is of no use to others.

![deploy-django-website-for-free](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ebd7mnecc8gq6z1z1cs1.png)

In this tutorial I will share the steps to deploy your django website for free on **[Railway](https://railway.app/)** which is my favorite website to host django websites for free.

## What is Railway?
Railway is a cloud-based platform that allows you to set up infrastructure, develop your app using that infrastructure on your local machine, and then deploy your app to the cloud. Railway app makes it extremely easy to host your projects online by importing projects straight from GitHub. In addition, Railway provides fast hosting for users' projects online. **Railway provides 500 hours of deployment each month for free in its Starter Plan which can be used to host your personal django projects.**

## Railway Features
Railway provides various features such as Automagic Builds, Multiple Environments, Deployment Rollbacks and so on.

1. **Automatic Builds**: Each time user pushes the code to GitHub, railway will automatically detect the changes and will build the project to deploy it.
2. **Multiple Environments**: Users can easily evolve their app overtime with the help of with fork joinable environments.
3. **Deployment Rollbacks**: In case the change made to the app has a bug or the user don't like the change, for that case railway supports one-click, instant rollbacks for every change.

## How to Deploy Django Website for Free in 2023
Moving forward to host django website for free on Railway, first you need a working django project. In case you do not have any, feel free to fork one from my [GitHub](https://github.com/ashish-makes) repos.

To Deploy Django Website for Free in 2023 follow the steps mentioned below:

- Firstly install a python library named gunicorn with the following command.

```
pip install gunicorn
```

- In next step, once your project is ready to deploy, create a file named **`Procfile`** without any extension in the root directory of the project. Add the following code in the file.

```
web: gunicorn project.wsgi
```
**Note**: Make sure to change **`project.wsgi`** to **`yourprojectname.wsgi`**

- Now create a file named **`requirements.txt`** which contains all the modules required for the project to run. In the root directory of the file run the following command using terminal.

```
pip freeze > requirements.txt
```
- The next step is to manage the static files during the production. To server static files during production we will use another python package named **`whitenoise`**. Install the latest version of the package using the command below.

```
pip install whitenoise
```
- Once the module is installed add the following middleware in the settings.py file just below the **`django.middleware.security.SecurityMiddleware`**

```
MIDDLEWARE = [
    # ...
    "django.middleware.security.SecurityMiddleware",
    "whitenoise.middleware.WhiteNoiseMiddleware",
    # ...
]
```
- Now add the following code in the settings.py file.

```
STATICFILES_STORAGE = 'whitenoise.storage.CompressedStaticFilesStorage'
```
- This step will tell the django to serve the staticfiles in the production. Add the following code to the settings.py file.

```
STATIC_URL = 'static/'

STATICFILES_DIRS = [
    os.path.join(BASE_DIR, "static")
]

STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
```
- Once all the above steps are completed, run the command below.

```
py manage.py collectstatic
```
- This step finishes all the important steps required to Host Django Website for Free.
- Now commit all the changes and push the code to GitHub.
- Visit Railway website and click on **Start a New Project**.
- On the next page click in **Deploy from GitHub repo** and login via GitHub account and give all the necessary permissions required.

![host-django-website-for-free](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kgaguzdw0x1gbyhuiexk.png)

- Once its done, now you can see your project repo in the list, select the project and click on **Deploy Now** button.
- After few seconds your Django Project will be hosted on the railway app for free. Now you can access the hosted django website from anywhere.
- Railway by default gives a long website URL which can be edited by settings section under domain option. Click in the edit icon and add any of your desired domain.

![deploy-django-website-for-free](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6beo94lmn1trsh0b9kga.png)

## Conclusion
Railway is the quickest way to see your django project in action on the internet. I tried to explain the steps in detail to Deploy Django Website for Free. However, in case of any query feel free to comment your query below.

**LinkedIn**: https://www.linkedin.com/in/ashish-makes/
**GitHub**: https://github.com/ashish-makes/