[Skip To Content]

Knowledgebase

Setting up Django

All hosting accounts come ready to make use of Django, a popular web application framework. Django is configured to use mod_fcgid rather than mod_python on our servers, so setting it up is a little different to the instructions on the Django web site. Django can be set up in three simple steps:

  1. First, upload your Django project to somewhere outside of "public_html". We recommend creating a "django" directory in your home directory, and placing your projects inside there. These instructions assume that this is where you've uploaded your code. You will need to run "python manage.py syncdb" in SSH after uploading your project (see the notes at the end for more on that).
  2. Create a file in "public_html" (or in the directory of the relevant subdomain or addon domain if you're not setting up Django on your main domain) called "django.fcgi". This file should be made executable (chmod 755), and should contain the following:
    #!/usr/bin/env python
    import sys, os
    
    # Add a custom Python path.
    sys.path.insert(0, "/home/username/django")
    
    # Set the DJANGO_SETTINGS_MODULE environment variable.
    os.environ['DJANGO_SETTINGS_MODULE'] = "projectname.settings"
    
    from django.core.servers.fastcgi import runfastcgi
    runfastcgi(method="threaded", daemonize="false")
    Replace username with your Web Site Control Panel/SSH user name and projectname with the name of your Django project.
  3. Finally, edit the ".htaccess" file in the same directory as "django.fcgi" (creating the file if it does not already exist) and add the following to it:
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ /django.fcgi/$1 [QSA,L]

You should now see your Django project when visiting your web site. Note that any changes to your Python code will not be immediately picked up, due to a bug in mod_fcgid. To get around this, after updating your files you should run "killall django.fcgi; killall python" in SSH to force any FCGID processes to exit.

Admin media files

If you're planning to use the built-in admin app, then you should do the following to get the media files working:

  1. Set ADMIN_MEDIA_PREFIX='/admin-media/' in settings.py.
  2. Create a symbolic link in the public_html directory (or in the relevant web root if you're setting this up for a subdomain or add-on domain):
    $ ln -s /usr/lib/python2.4/site-packages/django/contrib/admin/media/ ~/public_html/admin-media

Database configuration and running syncdb

When using Postgres, instead of relying on syncdb to create your tables it is advisable to use sqlall to get the SQL commands for each of your applications and create everything via phpPgAdmin. Letting syncdb create the tables can cause permissions problems if you wish to modify them later.

When using MySQL, to run syncdb you must set the DATABASE_HOST variable in settings.py to "127.0.0.1" instead of letting it default to "localhost". This forces Python to connect to MySQL via TCP rather than relying on the UNIX socket (which is not available in SSH).

Debugging errors

Any errors generated by your Django applications will be logged into the "tmp/error.log" file in your home directory. This includes any backtraces from unhandled exceptions, and any other text output on stderr.




Was this answer helpful?



Get Support