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

Running a Django Application on Windows Server 2012 with IIS

This is a first for me since under normal circumstances we run all our Django applications on Linux with Nginx, but we're in the process of developing an application for another department and due to the requirements around this project, we'll be handing the code off to them to deploy. They don't have any experience with Linux or web servers other than IIS, so I recently took up the challenge of figuring out how to run Django applications on Windows Server 2012 with IIS.

Based on the dated or complete lack of information around this I'm assuming it's not something that's very common in the wild, so I thought I'd share what I came up with in case others need to do this.


This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.

Assumptions and CaveatsThe operating system is Windows Server 2012 R2, 64-bit. If another variant of the operating system is being used, these instructions may not work properly.All of the soft…

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 …

Fixing DPI Scaling Issues in Skype for Business on Windows 10

My setup for my day job these days is a Surface Pro 4 and either an LG 34UC87M-B or a Dell P2715Q monitor, depending on where I'm working. This is a fantastic setup, but some applications have trouble dealing with the high pixel density and don't scale appropriately.
One case in point is Skype for Business. For some reason it scales correctly as I move between the Surface screen and the external monitor when I use the Dell, but on the LG monitor Skype is either massive on the external monitor, or tiny on the Surface screen.
After a big of digging around I came across a solution that worked for me, which is to change a setting in Skype's manifest file (who knew there was one?). On my machine the file is here: C:\Program Files\Microsoft Office\Office16\LYNC.EXE.MANIFEST
And the setting in question is this:
<dpiAware>True/PM</dpiAware>
Which I changed to this: <dpiAware>False/PM</dpiAware>
Note that you'll probably have to edit the file as administr…