Deploying a Django Website for free in 2024: A Step-by-Step Guide to Free Hosting
Published: 2024-01-09 06:30:30


Have you successfully developed your Django website and are eager to showcase it online? No worries, this guide will comprehensively explain how to deploy your Django website for free in 2023. While there are various platforms available for hosting Django projects, this tutorial will focus on my preferred choice—**_Railway_**. 


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

**Introduction:**
Mastering the skill of deploying websites is crucial for your web development journey. Deploying a Django website is a pivotal step as it makes the website accessible to users. Without deployment, the website remains confined to the developer's local machine, serving no purpose to others.


**What is Railway?**
Railway stands out as a cloud-based platform facilitating the setup of infrastructure, local development, and seamless deployment of your app to the cloud. It simplifies the hosting process by allowing direct project imports from GitHub. Additionally, Railway offers fast hosting services and provides 500 hours of free deployment each month in its Starter Plan, ideal for hosting personal Django projects.

Railway Features:
Railway boasts various features, including Automagic Builds, Multiple Environments, Deployment Rollbacks, and more.

Automatic Builds: Railway automatically detects code changes on GitHub and initiates project builds for deployment.
Multiple Environments: Users can effortlessly evolve their app over time using fork-joinable environments.
Deployment Rollbacks: In case of bugs or undesirable changes, Railway supports one-click, instant rollbacks for every modification.
How to Deploy Django Website for Free in 2023:
To host your Django website for free on Railway, follow the steps outlined below:

1. Install the Python library named Gunicorn with the command: `pip install gunicorn`.
2. Create a file named Procfile (without any extension) in the project's root directory and add the following code:  
`web: gunicorn project.wsgi`. Note: Replace project.wsgi with your actual project name.
3. Create a requirements.txt file containing all required modules with the command: `pip freeze > requirements.txt.`
4. 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')`
```
5.Run the command: 

```
`py manage.py collectstatic`
```
 to collect static files.
6.Commit and push all changes to GitHub.
7.Visit the Railway website, start a new project, deploy from the GitHub repo, and authorize necessary permissions.
8.Select your project, click "Deploy Now," and within seconds, your Django project will be hosted on Railway for free.
9.Customize the website URL under the domain options in the settings section.

Railway offers the quickest way to showcase your Django project online. This guide aimed to provide detailed steps for deploying your Django website for free.