So, as you know my side project is working with Python and Django. Well I have all the applications that I want for my project and now it is a matter of putting them together in my project. But before I do that I would like to test to see how Django works on my hosting provide as sort of testing the water so to speak.

Some slight words of caution: some people have reported problems with Django on Dreamhost. It is listed an as “Django-friendly web host” on the Django website, though, and I’ve personally not had any trouble. Also, the Dreamhost/Django setup involves running Django under FastCGI. While this is an officially supported configuration for Django, it’s not the recommended one — Django’s website states that mod_python under Apache is the preferred way. All that having been said, Dreamhost should be a perfectly adequate place for you to install and try out Django.

Prepare your Dreamhost account for Django

We’ll need to configure your Dreamhost account for use with Django. You can do this right alongside your existing website if you have one. Django will exist separately of it, so there should be no conflicts.

Add a subdomain

First, you’ll want to create a new subdomain for your Django application server. This way, you can play with Django all you like without affecting your production site. To create the subdomain:

  1. Open the Dreamhost administration panel.
  2. Choose “Manage Domains” under the Domains menu.
  3. Select “Add New Domain / Sub-Domain”
  4. Choose a name for your subdomain. I’ve chosen django.campbellgunn.com and will be using that name in this tutorial.
  5. Select both “Extra web security” and “Fast CGI Support”
  6. Click the ‘Fully host this domain now!” button.

Create a MySQL database

We’ll also need to setup a MySQL database to use with Django (sadly, Dreamhost doesn’t support PostgreSQL). Follow these steps:

  1. In the Dreamhost administration panel, choose “manage MySQL” under the Goodies menu.
  2. Enter a database name for your new database. I’ve chosen django_db and will be using that name in the tutorial.
  3. Enter a hostname for your database server. I’ve chosen djangodb.campbellgunn.com.
  4. Enter a username and password for your database.
  5. Click the “Add new database now!” button.

Create a few directories

We’ll need to create some directores for your Django install. You can do this however you like (shell, FTP, etc.), but it’s probably simplest to do it in the shell, as we’ll need the shell in a moment anyway. For these instructions, we’ll use the shell to create directories.

  1. SSH into your Dreamhost server (for me, it’s riddler.dreamhost.com, but yours is likely different).
  2. Make a new directory for Django-related bits: mkdir django
  3. Change into your new directory: cd django
  4. Create directories for templates and projects: mkdir django_templates and mkdir django_projects
  5. Create a directory to store your media files (css, images, javascript, etc.). Be sure to use the subdomain you’ve chosen, instead of django.jeffcroft.com: mkdir ~/django.campbellgunn.com/media

Download and set up Django

We’ll be using the latest Django by checking it out from its Subversion repository.

  1. In your ~/django directory (which you should already be in), check out the source: svn co http://code.djangoproject.com/svn/django/trunk/ django_src
  2. Edit your ~/.bash_profile file, adding the following lines. This sets your Python path to include your Django directories, and puts Django’s utilities in your system path. You can edit the file however you like.

.bash_profile Bash shell

export PATH$PATH:$HOME/django/django_src/django/bin export PYTHONPATH=$PYTHONPATH$HOME/django/django_src:$HOME/django/django_projects

  1. Activate your profile changes by “sourcing” the file: source ~/.bash_profile

Start a Django project

Django differentiates between “projects” and “applications.” A project can contain multiple apps. We’ll start by creating a project named “myproject.” If you choose a different name, you’ll need to modify the instructions accordingly.

  1. Move into your Django projects directory: cd ~/django/django_projects
  2. Start a new project, using Django’s command line utility. This creates a basic directory structure for the project and adds the necessary configuration files: django-admin.py startproject myproject
  3. Ensure that only you can read and write to the settings file for your project. This is very important, as your database password will be in this file: chmod 600 myproject/settings.py
  4. Edit the myproject/settings.py file with our configuration parameters. The parts of the file you’ll want to change are as follows:

Enter your name and e-mail address in the ADMINS setting:

settings.py 1 Python

ADMINS = (
# ('Your Name', 'your_email@domain.com'),
)

Enter your database settings, using the database name, username, password, and host your chose in the Dreamhost administration panel.

settings.py 2 Python
DATABASE_ENGINE = 'mysql'           # 'postgresql', 'mysql', 'sqlite3' or 'ado_mssql'.
DATABASE_NAME = 'django_db'             # Or path to database file if using sqlite3.
DATABASE_USER = 'cgunn'             # Not used with sqlite3.
DATABASE_PASSWORD = '123qwe'         # Not used with sqlite3.
DATABASE_HOST = 'djangodb.campbellgunn.com'             # Set to empty string for localhost. Not used with sqlite3.

Note: For MySQL, in case your DreamHost server does not have it already, you will also have to download and [easy_]install the MySQL-Python module, or else you will see error messages)

Edit your timezone:

settings.py 3 Python
# Local time zone for this installation. All choices can be found here:
# http://www.postgresql.org/docs/current/static/datetime-keywords.html#DATETIME-TIMEZONE-SET-TABLE
TIME_ZONE = 'America/Chicago'

Tell Django where your template directory is (we created it a while ago):

settings.py 4 Python

TEMPLATE_DIRS = (
# Put strings here, like "/home/html/django_templates".
# Always use forward slashes, even on Windows.
"/home/cgunn/django/django_templates"
)

Set the media root and media url to the media directory we created earlier:


settings.py 5 Python

MEDIA_ROOT='/home/cgunn/django.campbellgunn.com/media/'
MEDIA_URL='http://django.campbellgunn.com/media/'
Now, let’s edit your ~/.bash_profile file again, and add this line to the end of it.
.bash_profile Bash shell
export DJANGO_SETTINGS_MODULE=myproject.settings
  1. Then, active those changes by “sourcing the file”: source ~/.bash_profile
  2. Move into your project directory: cd ~/django/django_projects/myproject
  3. Syncronize your Django database with the changes you’ve made in the settings file. This process will also prompt you to create a “superuser” for your project: django-admin.py syncdb

Install and configure FastCGI

  1. Move into the directory for the subdomain you created way back at the begining: cd ~/django.campbellgunn.com
  2. Download the FastCGI Python script: wget http://svn.saddi.com/py-lib/trunk/fcgi.py
  3. Set the proper permissions on the script: chmod 755 fcgi.py
  4. Create a FastCGI Python script for your Django project. Make a new file called dispatch.fcgi and paste in these lines. Be sure to change the “cgunn” in the path to your username and the “myproject” to the name of your project, if you chose different name.
dispatch.fcgi
#!/usr/bin/env python
import sys
sys.path += ['/home/cgunn/django/django_src']
sys.path += ['/home/cgunn/django/django_projects']
from fcgi import WSGIServer
from django.core.handlers.wsgi import WSGIHandler
import os
os.environ['DJANGO_SETTINGS_MODULE'] = 'myproject.settings'
WSGIServer(WSGIHandler()).run()

  1. Set the proper permissions on dispatch.fcgi: chmod 755 dispatch.fcgi

Set up mod_rewrite

Apache’s mod_rewrite module will pass all requests to your website through dispatch.fcgi, and thus, into Django’s dispatcher.

  1. Create a file called .htaccess (nte the leading dot) in your subdomain directory (for me, django.campbellgunn.com). Add the following lines to the file:

.htaccess Apache Configuration

RewriteEngine On
RewriteBase /
RewriteRule ^(media/.*)$ - [L]
RewriteRule ^(admin_media/.*)$ - [L]
RewriteRule ^(dispatch\.fcgi/.*)$ - [L]
RewriteRule ^(.*)$ dispatch.fcgi/$1 [L]

  1. Reset Python to activate all of our changes: pkill python

All URLs on our subdomain should now be routed to Django. Feel free to open up your browser and try it out for yourself.

Install Django’s built-in administrative interface

Django’s awesome admin tool is a Django program itself. As such, we’ll need to install it into this project, just like we would any Django app. Note that it’s entirely optional. However, it really is one of the nicest things about Django, so there’s a good chance you’ll want it.

  1. Link Django’s admin media files into our subdomain directory be sure to use your subdomain, instead of django.campbellgunn.com): ln -s $HOME/django/django_src/django/contrib/admin/media $HOME/django.campbellgunn.com/admin_media
  2. Set the admin media prefix in django/django_projects/myproject/settings.py:

settings.py 6 Python

ADMIN_MEDIA_PREFIX = '/admin_media/'

Add the admin application to your installed applications in django/django_projects/myproject/settings.py:

settings.py 7 Python

INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.admin',
)
  1. Install the project with Django’s manage.py tool. You’ll need to be in your django/django_projects/myproject directory: python manage.py syncdb
  2. Uncomment the admin line in the URL configuration file (django/django_projects/myproject/urls.py):

urls.py Python

# Uncomment this for admin:
(r'^admin/', include('django.contrib.admin.urls')),

1. Reset Python again: pkill python

At this point, you should be able to go to http://django.yourdomain.com/admin and log into the Django administrative interface with the superuser username and password you created.

Now, you can get started making Django apps. From here, I highly recommend you head over to the tutorial on the official Django website. It’ll take you through build your first Django application. Note that we’ve already set up your first project, so you can skip the first section and head straight to “Creating Models.”

 

I have started doing extensive development with Django and come across an issue with changing the schema (models) frequently.

Lucky for me after looking around on the Internet and some help from Bing. I came across South, a migrations library for Django, that may help in solving problems with models as my web site evolves.

South is found at the following web site: http://south.aeracode.org.

I will continue to use South and update my blog with any big issues or other comments I find with my development.

 

Over at the Uswaretech Blog, I found an interesting piece of information.

The url is http://djangodesignpatterns.uswaretech.net/index.html.

Here is a quick blurb on it.

‘This is a collection of patterns which we have found occuring commonly with Django. All of these either make collaboration easier, coding simpler or code more maintainable. None of them are design patterns in the sense of GoF design patterns. We call them design patterns as none other seem closer or more convenient.

These are guidelines, which need to be overridden (very commonly, in some cases). Use your judgement when using them. As PEP8 says, “Foolish consistency is the hobgoblin of small minds.”’

I like how the chapters are laid as it is pretty much the way you progress in building a Django web site. (Notice that I did not say project or app, as these are very different meanings in Django.)

© 2011 Campbell Gunn Suffusion theme by Sayontan Sinha