The Wayback Machine - https://web.archive.org/web/20240915073419/https://www.geeksforgeeks.org/how-to-deploy-django-project-on-pythonanywhere/
Open In App

How to Deploy Django project on PythonAnywhere?

Last Updated : 07 Nov, 2023
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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 PATH

Cloning 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

Adding Tags Using Django-Taggit in Django Project
Django-Taggit is a Django application which is used to add tags to blogs, articles etc. It makes very easy for us to make adding the tags functionality to our django project.Setting up Django Project Installing django-taggit pip install django-taggitADD it to Main Project's settings.py file C/C++ Code INSTALLED_APPS = [ 'django.contrib.admin', 'dja
2 min read
How to Deploy Django application on Heroku ?
Django is an MVT web framework used to build web applications. It is robust, simple, and helps web developers to write clean, efficient, and powerful code. In this article, we will learn how to deploy a Django project on Heroku in simple steps. For this, a Django project should be ready, visit the following link to prepare one:https://www.geeksforg
4 min read
Deploy an ASGI Django Application
ASGI, which stands for Asynchronous Server Gateway Interface, is a big deal in Django. It basically helps Django handle lots of things at once, like requests from users. Instead of waiting for one thing to finish before starting the next, ASGI lets Django do multiple tasks simultaneously. It's like being able to juggle multiple balls instead of jus
5 min read
How To Deploy a Django Application to Heroku with Git CLI?
Deploying a Django application to Heroku using the Git command-line interface (CLI) is a simple process. Heroku provides a platform-as-a-service (PaaS) that enables you to deploy, manage, and scale your applications easily. This guide will walk you through the steps to deploy your Django application to Heroku using Git CLI. Deploying Django project
3 min read
How to customize Django forms using Django Widget Tweaks ?
Django forms are a great feature to create usable forms with just few lines of code. But Django doesn't easily let us edit the form for good designs. Here, we will see one of the ways to customize Django forms, So they will look according to our wish on our HTML page. Basically we will check the methods to include our own custom css, classes, or id
3 min read
Styling Django Forms with django-crispy-forms
Django by default doesn't provide any Django form styling method due to which it takes a lot of effort and precious time to beautifully style a form. django-crispy-forms solves this problem for us. It will let you control the rendering behavior of your Django forms in a very elegant and DRY way. Modules required:django : django installdjango-crispy
1 min read
Integrating Django with Reactjs using Django REST Framework
In this article, we will learn the process of communicating between the Django Backend and React js frontend using the Django REST Framework. For the sake of a better understanding of the concept, we will be building a Simple Task Manager and go through the primary concepts for this type of integration between React js and Django. Reactjs in a nuts
15+ min read
How to Fix Django "ImportError: Cannot Import Name 'six' from 'django.utils'"?
After Django 3.0, django.utils.six was removed. The "ImportError: Cannot Import Name 'six' from 'django.utils'" error occurs because of the dependency issue. This happens because when the project tries to use something that’s no longer available. Don’t worry—it’s easy to fix. We’ll explain why this error happens and give you simple steps to solve i
3 min read
Django Introduction | Set 2 (Creating a Project)
Note- This article is in continuation of Django introduction. Popularity of Django Django is used in many popular sites like as: Disqus, Instagram, Knight Foundation, MacArthur Foundation, Mozilla, National Geographic etc. There are more than 5k online sites based on Django framework. ( Source ) Sites like Hot Frameworks assess the popularity of a
3 min read
How to add RSS Feed and Sitemap to Django project?
This article is in continuation of Blog CMS Project in Django. Check this out here - Building Blog CMS (Content Management System) with Django RSS (Really Simple Syndication) Feed RSS (Really Simple Syndication) is a web feed that allows users and applications to access updates to websites in a standardized, computer-readable format. These feeds ca
3 min read
Django Project MVT Structure
Django is based on MVT (Model-View-Template) architecture. MVT is a software design pattern for developing a web application. MVT Structure has the following three parts - Model: The model is going to act as the interface of your data. It is responsible for maintaining data. It is the logical data structure behind the entire application and is repr
2 min read
How to Create a Basic Project using MVT in Django ?
Prerequisite - Django Project MVT Structure Assuming you have gone through the previous article. This article focuses on creating a basic project to render a template using MVT architecture. We will use MVT (Models, Views, Templates) to render data to a local server. Create a basic Project: To initiate a project of Django on Your PC, open Terminal
2 min read
Top 10 Reasons to Choose Django Framework For Your Project
When it comes to choosing a new language or framework for a project what matters to most of the developers? Simplicity? Reliability? Built-in packages to save a lot of time? Security? Rapid development? Community support? Versatility? or what?….. Well, we can’t deny that we always want a language or framework that can fulfill most of the requiremen
9 min read
How to add Site Header, Site Title, Index Title in a Django Project?
Automatic admin interface one of the most powerful parts of Django. Metadata is read from your models to provide a quick, model-centric interface where trusted users can manage content on your site. The admin's recommended use is limited to an organization’s internal management tool. It’s not intended for building your entire front end around. OVER
1 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.org/django-tutorial/ The below steps have to be foll
2 min read
Adding WYSIWYG editor to Django Project
Often, to manage content efficiently we use WYSIWYG (What You See Is What You Get) editor which stores our content in html and is also helpful to upload images, creating links, lists and works almost like Wordpress editor. This article is in continuation of Blog CMS Project in Django. Check this out here – Building Blog CMS (Content Management Syst
3 min read
Adding CSP headers in Django Project
Website security has been an important factor while developing websites and web applications. Many frameworks come with their own security policies and developers also try to implement the utmost security policies while developing their applications. Still even after this much hard work hackers will find new ways to penetrate into our app, exploit
5 min read
Django project - Creating a Basic E-commerce Website for Displaying Products
Project Title - Basic Ecommerce Website using Django Django is a powerful framework based on python. Here we will see how to create a basic e-commerce website in Django. This project includes storing products in the database and show them on the website. Refer to the following articles to check how to create a project and an app in Django. How to C
3 min read
How to enable CORS headers in your Django Project?
When site A wants to access content from another site B, it is called a Cross-Origin request. As it is disabled for security reasons, B sends an Access-Control-Allow-Origin header in the response. By default, a domain is not allowed to access an API hosted on another domain. If we want to allow our REST API (say backend) hosted in our Django applic
1 min read
How to create a new project in Django using Firebase Database?
Django is a Python-based web framework that allows you to quickly create efficient web applications. If you are new to Django then you can refer to Django Introduction and Installation. Here we are going to learn How to create a Django project using Firebase as Database . How to create a new project in Firebase ? Step 1: Firstly, We are going to Cr
3 min read
Create a new Django project in Pycharm using Pycharm Terminal
PyCharm is one of the most popular Python-IDE developed by JetBrains used for performing scripting in Python language. PyCharm provides many useful features like Code completion and inspection, Debugging process, support for various programming frameworks such as Flask and Django, Package Management, etc. PyCharm provides various tools for producti
2 min read
Joke Application Project Using Django Framework
Django is a high-level Python-based Web Framework that allows rapid development and clean, pragmatic design. It is also called batteries included framework because Django provides built-in features for everything including Django Admin Interface, default database SQLlite3, etc. Today we will create a Joke app in Django. Installation pip3 install dj
2 min read
Django Authentication Project with Firebase
Django is a Python-based web framework that allows you to quickly create efficient web applications.. When we are building any website, we will need a set of components: how to handle user authentication (signing up, signing in, signing out), a management panel for managing our website, how to upload files, etc. Django gives us ready-made component
7 min read
How to add Pagination in Django Project?
Pagination system is one of the most common features in blogs, search engine , list of result etc. Seeing the popularity of pagination system django developers have build a Paginator class so that web developers do not have to think of the logic to make paginators. Paginator Class live in django/core/paginator.py . So to use Paginator Class we firs
5 min read
Integrating Facebook Comments Plugin in Django Project
Django is a Python-based web framework that allows you to quickly create efficient web applications. It is also called batteries included framework because Django provides built-in features for everything including Django Admin Interface, default database – SQLlite3, etc. In this article, we will learn to integrate the Facebook comment plugin in Dj
2 min read
Wikipedia search app Project using Django
Django is a high-level Python based Web Framework that allows rapid development and clean, pragmatic design. It is also called batteries included framework because Django provides built-in features for everything including Django Admin Interface, default database – SQLlite3, etc. Today we will create joke app in django. In this article we will make
2 min read
Translator App Project using Django
Django is a high-level Python Web framework that encourages rapid development and clean, pragmatic design. Built by experienced developers, it takes care of much of the hassle of Web development, so you can focus on writing your app without needing to reinvent the wheel. It’s free and open source. Refer to the following articles to check how to cre
2 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 are publicly available. When a project is deploye
3 min read
Clone and Run a Django Project from Github
In this article, we will learn how to download any project from GitHub and deploy it to our local machine. You can clone any code or project from GitHub but for this article, we are cloning our Django project. What is GitHub?GitHub is an online platform where we can share our codes(or projects) online hassle-free, we can also host our local git rep
2 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 through the process of installing Django on a Windows m
5 min read