Great Python-based web framework. The home page is http://www.djangoproject.com/. There is a beta-book here: http://www.djangobook.com/. I've been going through this book to learn the Django framework. I have encountered a few problems that are simply due to some things changing in the Django framework that have not yet made their way into the beta-book. I want to document those items here.
Efficient Hierarchical Models with Django
click here for an example of how to model a hierarchical model structure. This is using the current (as of 2008-08-26) version of Django with the query-refactor stuff.
Using Django with mod_wsgi
URL Configuration File changes
In chapter 3, the syntax for specifying callbacks for various URL patterns has changed. Instead of this:
from django.conf.urls.defaults import *
from mysite.views import current_datetime
urlpatterns = patterns('',
(r'^now/$', current_datetime),
)
You should now use this:
from django.conf.urls.defaults import *
urlpatterns = patterns('',
(r'^now/$', "mysite.view.current_datetime"),
)
DJANGO_SETTINGS_MODULE error
In chapter 4, the author has you test the template system by starting a python session and entering this:
>>> from django.template import Template
>>> t = Template("My name is {{ my_name }}.")
>>> print t
When I did this I received this error:
EnvironmentError: Environment variable DJANGO_SETTINGS_MODULE is undefined.
To correct this I had to issue the following command before starting python:
export DJANGO_SETTINGS_MODULE=settings
This assumes one is in the root directory for your Django project. settings is the settings.py file that was created automatically when you started the project.
Update: In chapter 5 I found an even better solution... Instead of starting python with the basic python command, use python manage.py shell instead. This sets up everything so python already knows about the settings.
Specifying Location of Template directory
In chapter 4 this example is given on how to be able to use relative pathnames for the location of your template directories:
import os.path
TEMPLATE_DIRS = (
os.path.join(os.path.basename(__file__), 'templates'),
)
This doesn't work because the basename function returns the name of the current file. What we want is the directory that contains the current file. By the way, this is the settings.py file. So you should instead use this:
import os.path
TEMPLATE_DIRS = (
os.path.join(os.path.dirname(__file__), 'templates'),
)
Installing the Python Imaging Library
Got this error message when running the command python manage.py validate. This was for the example books application in chapter 5.
books.author: "headshot": To use ImageFields, you need to install the Python Imaging Library. Get it at http://www.pythonware.com/products/pil/ .
So I downloaded and installed it as follows:
- Download the source from the URL specified in the error message
- Unpack it, cd to the directory, and run
sudo python setup.py install
Using Django with DreamHost
Click here to read how I configured Django to work with my Dreamhost account
