Knowledgebase: Python
Create a Python 'Hello World' App with Django
Posted by ASO Admin on 11 February 2020 08:15 AM

Note: this tutorial is focused on Shared hosting environments only.

Preparing the Python Environment

The first thing we need to do is set up a local Python environment. For this we'll be using VirtualEnv, which is commonly used to isolate Python environments. This tutorial will require SSH access to your account, which is covered <here>. Also, in several places we refer to your account's home directory, and use "%USER%" (without the quotes) to refer to your account username. Make sure to replace every occurrence of "%USER%" with your actual account username.

First, log in to your account via SSH. If you're already logged in, let's ensure we're in the base of your home directory by entering:

cd ~

Next we'll initialize our local python environment under the "pyenv" directory, relative to your home directory (/home/%USER%/pyenv):

virtualenv --python=python2.6 pyenv
source pyenv/bin/activate


pip install django

Creating Your First Django Application

Just in case you're revisiting this tutorial, please ensure we're still in or change to your account's home directory:

cd ~

Now that our environment has been set up to support Django, we can create our first Django based project. For this tutorial we're going to create a standard Hello World application. First, let's create our django project:

~/pyenv/bin/ startproject whyhellothere

And now change to our application's directory:

cd whyhellothere

Now we need to finish creating our application directory layout:

mkdir public
mkdir tmp

Now, let's create a view for our application:

vi whyhellothere/

Inside this file, place these contents to create our first view:

from django.http import HttpResponse
def hello(request):
    return HttpResponse("Hello World")

Next, we need to connect our view to a URL:

vi whyhellothere/

At the top of this file, directly below the line "from django.conf.urls import patterns, include, url", add the following:

from whyhellothere.views import hello

Then, later in the file, you'll see "    url(r'^admin/', include(,". Directly above this line, add:

url(r'^$', hello),

Save and then close the file. Next we need to set up our static assets to serve them properly, open the following file:

vi whyhellothere/

At the bottom of the file, you should find a line that looks like "STATIC_URL = '/static/'", we need to change this to:


And directly below that line, add:

STATIC_ROOT = '/home/%USER%/whyhellothere/public/'

Save the file and close it. Now you'll need to collect the static assets from your new Django app. Run the following command:

~/pyenv/bin/python collectstatic

It should prompt a yes or no question about replacing files in the directory we gave above for the STATIC_ROOT setting. Enter 'yes'.

Set Up Your New Django App for Passenger

We're almost done! Next, we need to create a file named "". This is the entry file to our application:

vi ""


import sys, os

os.environ['DJANGO_SETTINGS_MODULE'] = "whyhellothere.settings"
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()

Save and close the file. The final step is to connect our application to our domain's document root.

Please be mindful that this involves replacing the directory with a symlink, so the document root must be empty to avoid file loss! If you have other domains or content hosted on your account, and you're setting this up for your primary domain, please contact support in regards to changing your document root first!

If this is the only domain on your account and no other content is hosted here, it's safe to proceed as is, and we're assuming "public_html" is the document root in question. Adjust the directory for a different document root!

First, remove the old directory:

rm -fr /home/%USER%/public_html

Replace it with a symlink to our application's public folder:

ln -s /home/%USER%/whyhellothere/public /home/%USER%/public_html


(0 vote(s))
This article was helpful
This article was not helpful

Help Desk Software by Kayako fusion