Django has become one of popular frameworks over the past few years. Often, after creating your django project, you are confused, how to share it with people around you. This article revolves around how you can host your django application on pythonanywhere for free. So let’s get started ..!!
Step 1: Setup your Django Project (Local Changes)
Let’s create a simple application in Django for showing the deployment.
- Initialize your Django Project
django-admin startproject deploy_on_pythonanywhere
- Open project in your editor and under settings.py make:
ALLOWED_HOSTS = ['*']
- Create requirements.txt file using the command
pip3 freeze > requirements.txt
- File structure of our project looks like this:
deploy_on_pythonanywhere ├── db.sqlite3 ├── deploy_on_pythonanywhere │ ├── asgi.py │ ├── __init__.py │ ├── __pycache__ │ │ ├── __init__.cpython-38.pyc │ │ ├── settings.cpython-38.pyc │ │ ├── urls.cpython-38.pyc │ │ └── wsgi.cpython-38.pyc │ ├── settings.py │ ├── urls.py │ └── wsgi.py ├── manage.py └── requirements.txt
- Let’s Start our Django Server
cd deploy_on_pythonanywhere python3 manage.py runserver
Yahooooo, your server is running on localhost 🙂 But wait, wait… that’s not over, let’s make it live for the world
Step 2: Upload Project to GitHub
- Follow this link to push the project on github – How to Upload a Project on Github?
Step 3: Deploy Project on pythonanywhere
- Create an account on pythonanywhere – Click Here
- After Register, you can see the page like this
- Now click on Console then select Bash you’ll see like this
- Run following commands on bash:
- Clone GitHub repo
git clone https://github.com/Prakhar-Mangal/deploy_on_pythonanywhere.git
- Now create and setup environment variables
python3 -m venv env #create virtual environment source env/bin/activate #activate virtual environment cd deploy_on_pythonanywhere #navigate inside your project pip install -r requirements.txt #installing dependencies using requirements.txt
- Now copy the path of your directories which you installed on bash
- Type command on bash
cd ls # get list of directories pwd #copy the path for future use
- Here it looks like:
Hurree, we set up our project successfully : ) But wait wait, follow the final process and we’re ready to go
- Now click on Web then select Add a new web app
- Click on next and follow the procedure
- select Django as the framework
- Select python3.8 (latest) and click on next till last.
- Now under the Web section open the WSGI configuration file
- Edit WSGI configuration file on line no. 12 and 17 remove the word mysite with your project name which you cloned from GitHub, in my case it is deploy_on_pythonanywhere
- Now it looks like this and then click on save:
- Select Virtualenv section under Web:
- Enter the path of Virtualenv as we created using bash (refer above pwd command for path)
- Click on Reload under the Web section and visit the link
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.