How to deploy a Django project for free – Django tutorials

Deploy Django project for Free

Hey Guys..! Welcome to another interesting tutorial. In this tutorial, you will know how to deploy a Django project for free.

Once we developed our Django project, we need to deploy it on the server so that the world can use it.

So what are you waiting for, lets give life to your Django project? To deploy a Django project for free I use Pythonanywhere. It’s absolutely free for 3 months, But you can deploy a single Django project. If you need more space or time you have to upgrade it.

Steps to Deploy a Django Project

Here, i gave a very few simple steps to deploy a Django project.

  • Create a Django Project
  • Upload your Django project on GITHUB
  • Create Pythonanywhere account
  • Create a New console
  • Create Virtual Environment
  • Clone your Django Project
  • Install requirements
  • Create a new web app
  • Configure static & media files
  • Configure WSGI file
  • Add Allowed Hosts

Let’s follow the each step to deploy our Django project.

1. Create a Django project

If you don’t know how to create Django project & configure it, please read this amazing Django tutorials.

Django tutorial – Build your first App with Django python 3

Django tutorial – how to change Django settings file

Django tutorials – how the django project works

I hope you will follow this amazing tutorials. let’s move.

2. Upload your Django project on GITHUB

I think you already know how to upload your Django project on Github, if you don’t know follow these commands.

1- git init
2- git add .
3- git commit -m "Add all my files"
4- git remote add origin https://github.com/USER_NAME/FOLDER_NAME # your repository
5- git push -f origin master

Don’t forget create your Github account.

3. Create Pythonanywhere account to deploy a Django project

let’s create a pythonanywhere account to deploy a Django project for free. Click that link and give the basic details, so that you will get access to the dashboard. It’s a free account soo we will get a single web application to deploy and few bash consoles.

4. Create a New console

Now, lets create a new bash console to work on our deployment. click that consoles tab and click that bash to create a new console.

5. Create Virtual Environment

let’s create a new python virtual environment to deploy and run our Django project smoothly. Here I gave a simple command to create a virtual environment. Please note the python version it will use further steps.

mkvirtualenv --python=/usr/bin/python3.7 <Enter env NAME >

# Here i am using Python 3.7 version.

Example:
mkvirtualenv --python=/usr/bin/python3.7 env_blog

If you execute it on your bash console, you will get the output like below.

6. Clone your Django Project

It’s time to clone your project from GitHub to your server environment. let’s run the below command to clone your Django project. You need change URL to your repository.

7. Install requirements

It’s time to install your project requirements to run your project smoothly, if you were created a requirements.txt file then execute the below command on the same bash console or you can manually install each and every package to the virtual environment.

Note: you need to root your BASH to where requirement.txt exists.

pip install -r requirements.txt # if requirements.txt exists in your project

# or

pip install django
pip install requests
etc..

8. Create a new web app

let’s create a new web app. Click on the Web tab. Click on add a new web app and click next.

Now, select Manual configuration because we already have Django project in our server.

Here, you need to click the exact python version which you gave in virtual environment and click next. It will take a minute to create a new domain with your username.

Once you done all the above process, it will show the below screen. your project now live that url, you can access but it will show welcome message because we need to configure our project.

Whenever you update your Django project you need to reload it, so that all new things will get update.

9. Configure static & media files

Now , we are ready to configure our Django project source code, static and media files. lets do it guys.

Here, you need to copy your Django project folder path which you cloned previously from GitHub. Open Files tab to see your project folder.

Let’s configure your virtual environment folder path here.

Here, we need to configure both static & media files like above.

10. Configure WSGI file

Let’s configure the WSGI file. Go to the code section, there you will find the WSGI Configure file. Now open that file, remove all the code in that file.

Now copy the below code and paste in that file, don’t forget to save that file.

import os
import sys

path = '/home/myusername/mysite' # Project Folder
if path not in sys.path:
     sys.path.insert(0, path)
os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings'  #project settings.py file
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

Observe it carefully.

11. Add Allowed Hosts

Now, let’s add hosts to access our Django project. Go to files and move to your project folder, open your project settings.py file, go to ALLOWED HOSTS. let’s edit this setting to allowed hosts and change DEBUG = FALSE.

12. Run Migrations

Now, run migrations. go to your bash console and run the below commands.

python manage.py makemigrations

python manage.py migrate

python manage.py collectstatic

Now reload your domain and run it on your browser. Finally,

we deployed it successfuly on pythonanywhere.

Please follow the above steps to complete your deployment successfully. I hope it will useful to you a lot. If you any quires please write a comment below and share this tutorial on social media.

Thank you.. !

Leave a Reply