How to install Django

This page is showing a generic answer.
To see a more detailed answer customized for you, type your domain name here:

Django is a Web framework written in Python that is designed to help build complex Web applications easily by reusing common code.

These instructions are designed to cover the specifics of setting up Django on our servers. Since we run a shared hosting environment that doesn't provide mod_python or root access, this guide will explain how to install and configure Django to run via FastCGI in a Virtual Python environment. Please be sure to consult the Django documentation for any further information.

Please note that Django is an advanced software package that is targeted at Web developers who are familiar with the Python programming language and comfortable with the command-line shell. We can’t provide support for custom Python scripts or versions. If you’re looking for a content management system for your Web site that is extendable and easy to manage, you may want to consider WordPress instead.

1. Install Python virtualenv

You’ll need to setup a "Virtual Python" installation, called a virtualenv. This will allow you to install additional Python modules without requiring “root” access.

First, find the current version of virtualenv. To do this, visit the virtualenv download page and note the version number on the top. It will be something like “1.11.6”.

Then login to the shell and run the following commands from your home directory, replacing “1.11.6” in the first line with whatever the current virtualenv version is:

VIRTUALENV_VERSION=1.11.6
/usr/bin/curl -O https://pypi.python.org/packages/source/v/virtualenv/virtualenv-${VIRTUALENV_VERSION}.tar.gz
tar xzf virtualenv-${VIRTUALENV_VERSION}.tar.gz
python virtualenv-${VIRTUALENV_VERSION}/virtualenv.py ~/private-python
rm -rf virtualenv-${VIRTUALENV_VERSION} virtualenv-${VIRTUALENV_VERSION}.tar.gz

This installs a private version of Python in a directory called “private-python”. Update your $PATH variable to use this private version by running this command:

echo 'export PATH=$HOME/private-python/bin:$PATH' >> ~/.bash_profile

To reload your bash_profile and verify that the change has taken effect, run the following commands:

source ~/.bash_profile
which python

It should display "/home/ex/example.com/private-python/bin/python".

By the way, the instructions above are simply a version of the more detailed instructions on the virtualenv installation page. if you have any trouble, you should consult the “To use locally from source” section of that page.

2. Installing extra modules

Before creating your first Django site, you need to install at least one additional Python module called "flup". Flup is a Python utility for FastCGI enabled sites. As mentioned above, our servers don't use mod_python, so FastCGI is used to run python scripts. You can install this package by running the following command:

pip install flup

Now you are ready to install Django.

3. Get Django

To get the latest stable version of Django, run this command:

pip install Django

You can verify Django is installed by running the following command:

python -c "import django; print django.get_version()"

If everything is working properly, this will print the Django version, such as “1.6.5”.

Django is now successfully installed on your account and you can start setting up projects. Below are instructions for creating a very simple test site, giving an idea of how to manage Django projects on our servers.

4. Creating a test site

Django developers recommend keeping the code for your sites outside of your main Web directory, so users aren't able to view any sensitive information that may be contained within your project. Create a “projects” folder in your home directory to store your projects:

mkdir ~/projects
cd ~/projects
django-admin.py startproject testsite

This creates a directory at "~/projects/testsite". Now you need to add a link to this folder in the local Python "site-packages" directory, so Python recognizes your new module:

ln -s ~/projects/testsite/ ~/private-python/lib/python2.7/site-packages/

Now you will create a subdomain in your Web folder which will let you access the project via a Web browser:

mkdir ~/html/testsite

Then switch to that folder:

cd ~/html/testsite

Now you need to create a FastCGI script to run this project. Using a text editor, create a file called "dispatch.fcgi" that contains the following code:

#!/home/ex/example.com/private-python/bin/python

import sys
import os

sys.path.append('/home/ex/example.com/projects/testsite')

os.environ['DJANGO_SETTINGS_MODULE'] = 'testsite.settings'

from django.core.servers.fastcgi import runfastcgi
runfastcgi(method="threaded", daemonize="false")

Once you have saved this file, make sure it has the correct permissions by running:

chmod 0755 dispatch.fcgi

Now create a .htaccess file in this folder with the following code inside:

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !(dispatch.fcgi)
RewriteRule ^(.*)$ dispatch.fcgi/$1 [L]

This lets the Web server know to run your dispatch.fcgi script when somebody visits the Web site. You should now be able to view your Django project by visiting http://testsite.example.com/.

5. Using a database

If you plan to use Django’s database API functionality, you’ll need to create a MySQL database. We recommend creating a new database specifically for each Django project, rather than using an existing one. To create the database on our servers, follow our instructions explaining how to create a MySQL database.

Django uses the MySQLdb package to interact with your MySQL database via Python, which you will need to install if using a database:

pip install mysql-python

Your database details can be entered in the settings.py file for your application.

6. Final notes

Managing sites with Django can be a complicated task that goes well beyond the scope of our support. For any questions regarding the use of this software, please see the official Django documentation for more detailed information and support.