Django

From DreamHost

Jump to: navigation, search

Django is a web development framework for Python (in the same way rails is a framework for Ruby). It is used by a number of major web sites, including Google (for Google Application Engine), and can make developing rich web applications much easier.

Django is not an application on its own, however. You will need proficiency in Python programming in order to write an application using Django. If you are not already familiar with Python, a good starting point is Dive Into Python.

Contents

Setup

Here's how to set up a Django site under your account using Passenger WSGI, which is our recommended method for doing so. Instructions for completing a FastCGI setup are still available at Django FastCGI, but are not recommended -- FastCGI is (ironically) significantly slower, and requires a more involved setup process.

Step 1: Set up a shell account

If you don't already have a shell account set up for your domain, you will need to do so now. See Enabling Shell Access for details.

Step 2: Configure your domain

Next, you will need to set up your domain to use Passenger. You can do so by following these steps:

  1. Open the Manage Domains section of the panel, and start editing your domain.
  2. Scroll down to the "Users, Files, and Paths" section. If your web directory does not end with "/public", add that to your web directory.
  3. Once you have done so, scroll down to "Web Options". Turn on the "Passenger" checkbox.

NOTE: If you currently have files under the domain, you will need to move them into the new "public" directory to make them appear!

Step 3: Create a database

Any Django site will need a database. If you don't have one prepared already, you can create one through the panel at MySQL Databases. We strongly recommend creating a database just for Django (rather than sharing a database with other applications), as Django does not use a prefix on its table names.

Step 4: Run the Django setup wizard

SSH into your server, cd into the directory for your application, and run these commands:

 wget http://wiki.dreamhost.com/django-setup.py
 python django-setup.py

This script will lead you through rest of the the process of setting up your application.

Hints

  • If your application isn't working, double-check usernames, passwords, database names, and hostnames in the settings file.
  • If you make changes to the code, such as working through the official tutorials, and they don't seem to work, make sure to kill any existing python processes and reload the page.
 pkill python

Performance Improvements

Shared environment forces users to use some tricks to improve overall performance of web applications. Django provides many ways to do it:

The latter two can be achieved by placing a specially crafted .htaccess in top static file directories (like media/, appmedia/). The one I use is here:

ExpiresActive on
ExpiresDefault "access plus 15 minutes"
ExpiresByType image/gif "access plus 1 hours"
ExpiresByType image/png "access plus 1 hours"
ExpiresByType image/jpeg "access plus 1 hours"
ExpiresByType image/x-icon "access plus 1 hours"
ExpiresByType application/x-javascript "access plus 15 minutes"
ExpiresByType text/css "access plus 1 hours"

SetOutputFilter DEFLATE
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.0[678] no-gzip
BrowserMatch \bMSIE\s6 no-gzip
BrowserMatch \bMSIE\s7 !no-gzip !gzip-only-text/html
SetEnvIfNoCase Request_URI \
        \.(?:gif|jpe?g|png|pdf|swf|ipk)$ no-gzip dont-vary
Header append Vary User-Agent env=!dont-vary

Warning! The above file works only for Apache 2.0! If you still use Apache 1.3, please talk to DreamHost support to switch you over to Apache 2.0.

Helpful Links

Personal tools