Django Gotchas

30 June 2006

One of the biggest hurdles in learning any new framework or programming language is learning its quirks. By quirks I don’t mean bugs, I’m referring to the personality and flow. Here are a few that might help your troubleshooting process:

I get an error when I run syncdb. This could be a number of things. If its an error in the model you should be able to tell. Most of the time for me was forgetting to add the project to the settings.py INSTALLED_APPS list.

I created a templatetag but Django is saying the file doesn’t exist. First make sure you have loaded the template tag, (i.e. {% load latestposts %}). Second, and this one gets me every time, make sure you added the __init__.py file to the newly created directory. This file must be present in directories that contain python scripts.

How do I access the children of a many-to-one or many-to-many relationship within the template? This puzzled me for a while. I’m accustomed to saying post.categories but in Django you must say post.categories_set.all. Referenced here.

I just added my database from my model but I keep getting a 404 error when I try to view the template. This drove me crazy and people will probably laugh. If you forget to enter data into the newly formed table Django will throw a Page Not Found error until you enter your first record of data. It’s always the small things.

I hope these are somewhat helpful. If you have any to add to the list feel free to post a comment.

Remarks

Jeff Croft http://www.jeffcroft.com/

Good tips, and all are things I’ve been “gotten” by, too.

On the last one, it’s worth noting that this is specific to Djangos’ generic views, and that there is an “allow_empty” parameter that will show you your template with a blank dataset, rather than a 404 if there are no records.

Generic Views

:)

Nathan Borror http://www.playgroundblues.com/

Very good point, thanks for the tip and link!

Baxter

As a total Django newb, my “gotchas” are outnumbering my “ahas” by about 3 to 1. Accessing children is just killing me.

That said, if I can just get over the learning curve I think me and Django are going to get along real well.

Jacob Kaplan-Moss http://www.jacobian.org

The init.py thing still bites me all the time!

I think I remember reading that Python 2.5 will issue a warning if you try to import from a module without an __init__.py; it would be a huge help.

SmileyChris

Curse you, infernal __init__.py!!

Orymy Lee

Holy hit, it happened again

Oarymy Lee

Can anyone make me a diabolo? or a god?

Zarra Ock

Strange things happens too many times to be just an occasion

macdet http://mobbing-gegner.de

@…. over the learning curve I think me and Django are going to get along real well. that’s me :)

Remarks are closed.

Remarks have been close for this post.