How to Deploy Django project on PythonAnywhere?
Last Updated :
07 Nov, 2023
Django has become one of the popular frameworks over the past few years. Often, after creating your Django project, you are confused, about how to share it with people around you. This article revolves around how you can host your Django application on Pythonanywhere for free.
Uploading your code to PythonAnywhere
PythonAnywhere provides free Python hosting Django projects and makes your Django project live to the world.PythonAnywhere is also very easy.
Uploading your code to PythonAnywhere is a straightforward process that allows you to run Python scripts and web applications in a cloud-based environment. Here's a step-by-step guide on how to upload your code to PythonAnywhere.
Step 1: Setup your Django Project (Local Changes)
Initialize your Django Project
django-admin startproject deploy_on_pythonanywhere
Open the project in your editor and under settings.py search "ALLOWED_HOSTS" and allow all hosts:
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

As you can see that, your server is running on localhost. But 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. 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
Problem faced while hosting python website on PythonAnywhere
Install all the required python packages before creating new web app in python anywhere so that you can avoid errors while hosting.
CSS,JS integration problem : It occurs when we did not given the path of the static file in python anywhere web app so give the path of static file in STATIC FILE SECTION.
STATIC FILE PATHCloning GitHub website code problem : Sometimes when we clone website from GitHub it asks for username and password after giving the right username and password it still shows some error you can avoid this problem by creating classic token in GitHub you can take help from https://www.iorad.com/player/2052920/Github---How-to-generate-classic-personal-access-tokens-#trysteps-1 paste the token at password place.
Other webpages URLs' linking problem : If we used http://localhost:8000 in the website there is need to change this thing with your real path https://username.pythonanywhere.com/urlpathname otherwise when your website get hosted your other URLs present in html, JS etc. would not work.
Security problem : We can make our website secure using SECURITY of PythonAnywhere we just need to enable FORCE HTTPS.
SECURITY in WEBSITE
Similar Reads
How to Add Python Poetry to an Existing Project
Poetry is a powerful dependency management tool for Python that simplifies the process of managing the project dependencies, packaging, publishing, etc.. . It simplifies dependency management, virtual environments, and packaging, thus making development more efficient and less prone to errors. The p
3 min read
How to create a Django project?
Dive into the world of web development with Python by exploring the versatile Django framework. Django is a go-to for many developers due to its popularity, open-source license, and robust security features. It enables fast and efficient project development. In this tutorial, we will guide you throu
5 min read
Protecting sensitive information while deploying Django project
There will be a lot of sensitive information in our Django project resided in the settings.py, or local variables containing sensitive information or the POST request made using forms. So while deploying a Django project we always have to make sure they are protected especially the repositories that
3 min read
Top Python Projects on GitHub
Python has established itself as one of the most versatile and user-friendly programming languages in recent years. With a strong community and extensive libraries, it supports everything from web development to data science and machine learning. GitHub, a treasure trove for developers, offers numer
8 min read
How to Upload Project on GitHub from Pycharm?
PyCharm is one of the most popular Python-IDE developed by JetBrains used for performing scripting in Python language. PyCharm provides some very useful features like Code completion and inspection, Debugging process, support for various programming frameworks such as Flask and Django, Package Manag
3 min read
How To Add Unit Testing to Django Project
Unit testing is the practice of testing individual components or units of code in isolation to ensure they function correctly. In the context of Django, units typically refer to functions, methods, or classes within your project. Unit tests are crucial for detecting and preventing bugs early in deve
4 min read
How to Push your Django Project to GitHub
In this article, we have explained how to push a Django project on GitHub using Git and direct upload. Git is a version control system that allows us to track our changes and GitHub is a cloud-based platform where we can store, share, and manage your code or projects. With Git and GitHub we can coll
6 min read
Make PWA of a Django Project
Progressive Web Apps or PWA are a type of application that is developed using web technologies and can be installed on any device like a normal application. Prerequisites: A django project that is ready to be deployed. For Django tutorial you can refer the following link https://www.geeksforgeeks.o
2 min read
How To Access Pc Django Server In Android
Django server is mostly run on computers but when you want to make your Django project responsive and do various testing. Then using mobile can be proved as an advantage. Here we will try to learn how we can deploy our website on our phones. Stepwise ImplementationStep 1: we have to first enable Pyt
3 min read
College Management System using Django - Python Project
In this article, we are going to build College Management System using Django and will be using dbsqlite database. In the times of covid, when education has totally become digital, there comes a need for a system that can connect teachers, students, and HOD and that was the motivation behind buildin
15+ min read