Skip to main content

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.

Comments

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.

Popular posts from this blog

Installing and Configuring NextPVR as a Replacement for Windows Media Center

If you follow me on Google+ you'll know I had a recent rant about Windows Media Center, which after running fine for about a year suddenly decided as of January 29 it was done downloading the program guide and by extension was therefore done recording any TV shows.

I'll spare you more ranting and simply say that none of the suggestions I got (which I appreciate!) worked, and rather than spending more time figuring out why, I decided to try something different.

NextPVR is an awesome free (as in beer, not as in freedom unfortunately ...) PVR application for Windows that with a little bit of tweaking handily replaced Windows Media Center. It can even download guide data, which is apparently something WMC no longer feels like doing.

Background I wound up going down this road in a rather circuitous way. My initial goal for the weekend project was to get Raspbmc running on one of my Raspberry Pis. The latest version of XBMC has PVR functionality so I was anxious to try that out as a …

Setting Up Django On a Raspberry Pi

This past weekend I finally got a chance to set up one of my two Raspberry Pis to use as a Django server so I thought I'd share the steps I went through both to save someone else attempting to do this some time as well as get any feedback in case there are different/better ways to do any of this.

I'm running this from my house (URL forthcoming once I get the real Django app finalized and put on the Raspberry Pi) using dyndns.org. I don't cover that aspect of things in this post but I'm happy to write that up as well if people are interested.

General Comments and Assumptions

Using latest Raspbian “wheezy” distro as of 1/19/2013 (http://www.raspberrypi.org/downloads)We’lll be using Nginx (http://nginx.org) as the web server/proxy and Gunicorn (http://gunicorn.org) as the WSGI serverI used http://www.apreche.net/complete-single-server-django-stack-tutorial/ heavily as I was creating this, so many thanks to the author of that tutorial. If you’re looking for more details on …

The Definitive Guide to CouchDB Authentication and Security

With a bold title like that I suppose I should clarify a bit. I finally got frustrated enough with all the disparate and seemingly incomplete information on this topic to want to gather everything I know about this topic into a single place, both so I have it for my own reference but also in the hopes that it will help others.Since CouchDB is just an HTTP resource and can be secured at that level along the same lines as you'd secure any HTTP resource, I should also point out that I will not be covering things like putting a proxy in front of CouchDB, using SSL with CouchDB, or anything along those lines. This post is strictly limited to how authentication and security work within CouchDB itself.CouchDB security is powerful and granular but frankly it's also a bit quirky and counterintuitive. What I'm outlining here is my understanding of all of this after taking several runs at it, reading everything I could find on the Internet (yes, the whole Internet!), and a great deal…