Learn how to Host Django Application for free at no cost. Simple and Easy step for hosting Django Website on the Internet without any money within 5 minutes. We will be using Python Anywhere for hosting our application on Cloud. Free Hosting – host without cost which has Simple and Ready to use Python Environment Interface which doesn’t need to install anything.
Live Demo – http://professionalcipher.pythonanywhere.com/ [Comment if not working]
Deploying/Host Django Project on PythonAnywhere
Deploying a project on the server is more like setting up your local machine. We will be using virtualenv to create our Django Project Environment. In this tutorial, we will clone the project from Github which is a part of our Django CRM Tutorial.
Note – Before getting started Sign up to PythonAnywhere and Create Your Account. We will be using Beginners’ plan which is a free plan.
Here’s an overview of the steps involved.
- Upload your code to Hosting Cloud Server
- Set up a virtualenv and install Django and any other requirements
- Set up your web app using the manual config option
- Add any other setup (static and media files, env variables)
Uploading Django Code to Hosting Server
As our code is ready on GitHub, we will clone it using Bash Console. Create a Bash Console by clicking on the Console Tab. You will see the terminal interface where you need to type git clone command.
# for example git clone https://github.com/studygyaan/Django-CRM-Project.git
Create VirtualEnv and Install Django and Dependencies
In your Bash console, create a virtualenv, named after your project name, and choose the version of Python you want to use like we are using now 3.7:
mkvirtualenv --python=/usr/bin/python3.7 mysite-virtualenv
You will see now in terminal like this –
(mysite-virtualenv) $ which means virtualenv is activated. If you don’t then type bellow command which will activate it.
Once the Virtual Env is activated. We will install Django inside it. There are two ways to do it. If you have requirements.txt file then you can use following
pip install -r requirements.txt
Or simply install Django and Required Libraries
pip install django
Setting up your Django Web app and WSGI file
At this point, you need to be armed with 3 pieces of information:
- The path to your Django project’s top folder — the folder that contains “manage.py”, eg – /home/myusername/mysite
- The name of your project (that’s the name of the folder that contains your settings.py), eg mysite
- The name of your virtualenv, eg mysite-virtualenv
Create a Web Application with Manual Config
Go to Web Tab and Click on it. You will see create a web application button, click on it and follow instructions.
NOTE: Make sure you choose Manual Configuration, not the “Django” option, that’s for new projects only.
Once your web app is created, scroll down and you need to add some paths like shown in bellow image.
Note – Maker sure you add folders’ names accordingly to your project names.
1. Code –
2. Virtualenv –
Edit WSGI File to Point our Django Project to Server.
Click on the WSGI configuration file link. It will take you to your Hosted Django Project WSGI File. We need to add the following code. Just remove everything inside that file and paste the bellow code in it. Note you need to change bold code which your relevant paths.
import os import sys # assuming your Django settings file is at # '/home/myusername/mysite/mysite/settings.py' path = '/home/myusername/mysite' if path not in sys.path: sys.path.insert(0, path) os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings' from django.core.wsgi import get_wsgi_application application = get_wsgi_application()
Save the file.
That’s it. Now the file step is to go to Web Tab and reload the web app and click on the link of your web app.
Disallowed Host Error
If you get Disallowed Host at Django let just add ‘*’ in ALLOWED_HOSTS in settings.py file and Reload the Web App.