Saturday, August 4, 2012

Notes on Practical Django Projects (Part 1)

I'm currently making my way through Practical Django Projects by James Bennett and thought I'd share some notes and errata as I go along. (Side note: If you want a paper copy of this book, it's dirt cheap at abebooks.com; just make sure to get the second edition which was published in 2008).

I've done a small amount with Python here and there in the past (writing some scripts to handle very specific tasks or that used libraries available in Python that were better than what's available in Java) and always really liked the language, but I haven't ever looked into building webapps with Python until now.

So far I'm quite impressed. Having worked with Grails a little in the not-too-distant past I'm finding Django to be similar conceptually but a little simpler, and it seems to be extremely well thought-out and strike an excellent balance between forcing a convention on the developer and requiring just enough to configuration to maintain a lot of flexibility.

I'll save a more thorough review of Django for a future date when I have it under my belt better, but for now I wanted to point out a stumbling block I hit due to a minor lack of specificity in the book.

For those of you even less familiar with Django than I am, Django operates on the concept of a high-level project and within that project are one or more applications. Conceptually this is similar to what other frameworks might call a module, or kinda-sorta-but-not-completely like a Grails plugin. The point is that Django projects are built of multiple modular building blocks, which is a really great way of organizing things.

On my machine I'm building all this stuff in a ~/djangoprojects directory, so inside there I have a cms directory for the simple CMS that you build as one of the projects in the book. So ~/djangoprojects/cms is the project directory, and inside that directory is a cms directory (so ~/djangoprojects/cms/cms) for the CMS application that is part of this project.

This is the way it should be, I believe, since you first run a script to create the project, then navigate into the project directory and run another script to create the first application that will make up the project. (If anyone with Django experience reads this and what I'm saying here isn't accurate, I'd love to be corrected so I can learn.)

Beginning on page 43 you add a search system to the CMS, and following the modular convention you start things off by running the startapp script from the project directory.

On page 43 it reads:
So let's make this into its own application. Go to your project [emphasis mine] directory and type the following command: 
python manage.py startapp search

The problem is the book gets the terminology a bit wrong (based on my understanding of the terminology thus far at any rate) and this leads to an error which took me a little time to track down, probably largely due to my newbness with Django.

As directed I went into my project directory and created the search application, which means I wound up with a cms directory and a search directory at the same level in the project (which again, I believe is how it's supposed to be), so specificially I have these directories:
~/djangoprojects/cms/cms
~/djangoprojects/cms/search

Later you add a URL pattern to the project's urls.py file to tell Django what URL route you want to use to hit the search application:
(r'^search/$', 'cms.search.views.search')

When I ran the code, I was getting the error "ImportError at /search/ No module named search"

Note the path to the search application in the URL pattern above: cms.search.views.search

I probably should have noticed that right away since that indicates the search application would be inside the cms application, but since I created search in the project directory instead of the cms application directory, a simple change to the URL pattern to ommit the cms. at the beginning fixed the problem:
(r'^search/$', 'search.views.search')

As I said above all of this could be my own misunderstanding, and maybe the cms and search applications aren't supposed to be siblings, so if someone comments with a simple "you're wrong" that's a great way for me to learn. I did notice that Phil Gyford who was nice enough to put his completed source for the book online got it "right" so might just be me.

I'll share more notes/thoughts as I make my way through the rest of this (so far) excellent book, and if you have questions about any of this or thoughts you'd like to share about Python and Django I'd love to hear them.

5 comments:

Brandon Culpepper said...

You're wrong. :) j/k. Thanks for this. Now I have something new to play with.

Matt Woodward said...

Follow up comment -- honestly I'd skip this book. After about Chapter 3 it goes completely off the rails (ahem, so to speak) and is way too disorganized to follow. I'm starting up on "The Definitive Guide to Django" and some other resources now and thus far they seem a lot better.

woohoo said...

hm, same experience here, i took the same book, Practical Django Projects. since the book is old... or because a mistake in printing/editing, i got stuck at page 25... I cannot figure out how to fix the code (i am a beginner with django, but not with programming). Perhaps i will start with another django book like Matt did. too bad there are no up-to-date books about django at the beginning of 2013.

Nishant said...

Have you continued with django?

If you have, it would be cool if you could tell us what resources have worked for you.

lifo ed said...

Its such a delight when you've been scratching your head on an error for hours and uncle google lands you on a page that solves it.

Thanks Matt, I had stumbled on the same error and now I know what was causing it.