Django for the Impatient: Building a Blog
- Nov 25, 2008
Django bills itself as “the Web framework for perfectionists with deadlines.” So let’s put ourselves on deadline and see how fast we can produce a simple blog using Django. (We’ll address your perfectionist side later.)
All the work in this chapter is done on the command line in your shell of choice (bash, tcsh, zsh, Cygwin, or what have you). So open your terminal and cd to a directory that is on your PYTHONPATH environment variable. On a Unix-based system such as Linux, Mac OS X, FreeBSD, and so on, you can issue an echo $PYTHONPATH command to see its contents; from a Win32 Command window, type echo %PYTHONPATH%. You can read more about paths in both the installation and Python chapters.
We recommend you try to follow along and actually build the blog as you go. If that’s not practical—if you aren’t near a computer, or you’re just impatient—simply reading it is illuminating too. That’s especially true if you have experience with one or more other modern Web frameworks, since many of the basic concepts are familiar.
If you are following along on your own computer, and you reach a point where the results you’re getting don’t match what you see here, stop and re-examine the step you just completed, and then review the two or three steps before that. Look for a spot where you could have skipped over a seemingly unimportant detail or didn’t understand a specific instruction. If no light bulbs come on, delete your sample project and start over. The authors used this method when learning Django; in addition to being faster than staring blankly at error messages for hours, the repetition of the steps leading up to your trouble spot really help with your retention!
Creating the Project
The easiest way to organize your Django code when you are starting out is to use what Django calls a project: A directory of files that constitute, usually, a single Web site. Django comes with a utility called django-admin.py to streamline tasks such as the creation of these project directories. On Unix, it has a default installation into the /usr/bin directory, and if you’re on Win32, it goes into the Scripts folder right in your Python installation, for example, C:\Python25\Scripts. In either case, you need to make sure that django-admin.py is in your PATH so it can be executed from the command line.
To create the project directory for your blog project, issue this django-admin.py command:
django-admin.py startproject mysite
On a Win32 box, you need to open a DOS Command window first. It can be accessed via Start -> Programs -> Accessories -> Command Prompt. Also, instead of a $, you see something like C:\WINDOWS\system32> as a shell prompt.
Now take a look at the contents of the directory to see what this command has created for you. It should look something like this on Unix:
$ cd mysite $ ls -l total 24 -rw-r--r-- 1 pbx pbx 0 Jun 26 18:51 __init__.py -rwxr-xr-x 1 pbx pbx 546 Jun 26 18:51 manage.py -rw-r--r-- 1 pbx pbx 2925 Jun 26 18:51 settings.py -rw-r--r-- 1 pbx pbx 227 Jun 26 18:51 urls.py
If you were developing on a Win32 platform, opening an Explorer window to that folder looks something like Figure 2.1, if we created a folder named C:\py\django with the intention of putting our project there.
Figure 2.1 mysite folder on Win32
Besides __init__.py, the startproject command has created three other files.
- manage.py is a utility for working with this Django project. You can see from its permissions flags in the directory listing that it is executable. We run it in a moment.
- settings.py is a file containing default settings for your project. These include database information, debugging flags, and other important variables. Any value in this file is available to any of your project’s installed apps—we show you the usefulness of that as we progress through this chapter.
- urls.py is what’s known in Django as a URLconf, a configuration file that maps URL patterns to actions your applications perform. URLconfs are an exciting and powerful feature of Django.