How to set up a Django testing environment
Entry updated Feb. 15, 2008 at 8:54 a.m.
Folks following my Twitter updates know that I'm waist deep into Django at the moment. And I hope to be in over my head soon with a new project I'm working on (don't have much to show yet).
In the spirit of the Coast Guard, here is a brief guide for setting up a local Django development environment on your Mac.
NOTE: Please keep in mind that I'm quite the newbie when it comes to programming and anything resembling a server. If you have any suggestions on improving this entry, post it in the comments.
Before you begin
There are a few things you'll need before setting up your Django development environment. These things are, in no particular order: Python (at least 2.3), a database adapter (and accompanying database) and Django.
And if you're not familiar with using the command line via Terminal, now is your chance. It's like using DOS again!
Preparing your environment
Most Macs should come with Python pre-installed. You can see if this is true by opening Spotlight and typing in "Python." Open the Python folder, and you should be presented with another folder showing your version number (my iBook G4 had version 2.3).
If you don't have Python installed, or have a version earlier than 2.3, you'll need to download and install the latest version from the Python Web site. I'll be using version 2.4.4 for this tutorial.
Once Python is installed, you'll need to download and install your database adapter. I'm using MySQL for development via Dreamhost. Simply grab the pre-built installer package for MySQL.
Next you'll want to download the latest version of Django (0.96 as of this entry) from the Django Project Web site. If you have StuffIt Expander installed, you'll have the option to untar the downloaded file. If not, open Terminal and use the following command:
tar xzvf Django-0.96.tar.gz
Iif you downloaded the file to your desktop, make sure you change into that directory first by typing:
cd desktop
You can also follow the installation directions on the Django Project site.
Change into the directory you just created:
cd Django-0.96
Type the command:
python setup.py install
Begin your project
Create your project by changing into the directory where you want it to be located, and typing:
django-admin.py startproject myprojectname
Now you should be able to fire up the Django testing server by typing the command:
python manage.py runserver
Go to the the URL displayed in your shell (Terminal) in your browser (it will be http://127.0.0.1:8000 unless you change ports) and should see a light blue screen with the words "It worked! Congratulations on your first Django-powered page."
Start your application by switching back to your shell, and using the command:
python manage.py startapp myappname
As with your project, Django will create the necessary file structure inside your project.
Configure your database settings
Open the "settings.py" file located in your project folder in a text editor. Change the database settings (DATABASE_ENGINE, DATABASE_NAME, etc) to match those of the database you intend to use for testing.
(Since setting up that database will vary greatly depending on your hosting server, I'll not cover it here. Do make sure to add your computer's IP address to those with access to the database.)
Under the "INSTALLED_APPS" list, add your application like this (yes the comma is important):
'myproject.myapp',
Also, make sure this is listed:
'django.contrib.admin',
Next open your "urls.py" file and uncomment (remove the #) from the line following the one telling you to "uncomment this for admin."
Finally, sync your database by using the command:
python manage.py syncdb
This will also give you the opportunity to create your "superuser" admin.
Run the test server again, and append "admin" after the test server URL to bring up the Django admin login. Use the superuser username and password to log into the admin area. You should now be able to browse the Django admin interface.
Next steps
One final step in setting up your Django testing environment is to install the Python Docutils library. This is needed to view the Django documentation (look in the upper right-hand corner of your admin).
To do this, download the latest release and unpack it to your desktop. Open your local shell, and change into the directory for docutils.
Install the library by typing the command:
python setup.py install
Of course, there is much more to configuring your testing environment than what I've covered. And do note that I am definitely not an expert in this area -- [visit the Django Google Group}(http://groups.google.com/group/django-users Django Users Group on Google Groups") or IRC for more informed help.
Much of what I've posted here came from sources I've tagged with "django" on my del.icio.us.
Sitepoint also has an excellent article written by James Bennett that covers much of what I mention here, but in the scope of creating an actual application.

No comments
Comments no longer accepted for this entry.
To prevent spam, comments are no longer allowed after 60 days.