Monday, January 28, 2008

This Week in Django 8

with hosts Michael Trier and Brian Rosner

Bookmark and Share
!>http://blog.michaeltrier.com/assets/2007/12/14/twid_small.png!

This Week in Django is a weekly podcast about all things Django.

This week we have our special guest Matthew Wensing on the show to discuss his work with the Django GIS Branch (GeoDjango). We also cover EveryBlock, Some cool projects from the community, the Tip of the Week, and a couple of questions from the IRC.

Please see the Show Notes below for all the pertinent information and links

Downloads

AAC Enhanced Podcast (32.9 MB, 58:00, AAC)

MP3 Edition (39.9 MB, 58:00, MP3)

The Enhanced Podcast version contains screenshots and easy access links to all of the items I discuss throughout the podcast.

Feeds Available

iTunes Feeds are available, we have just failed to mention it. So please choose the iTunes feed if you want automatic downloads in iTunes. Please show us some iTunes love.

iTunes Feeds

This Week in Django – AAC Edition

This Week in Django – MP3 Edition

Regular RSS Feeds

This Week in Django – AAC Edition

This Week in Django – MP3 Edition

Give Us Feedback

Want to give us some feedback on the show? We’re always looking for ideas or suggestions that will help improve each episode. Please contact us at feedback <em>at_url</em> thisweekindjango.com.

Show Notes

Big News (1:03)

  • EveryBlock The brain child of Adrian Holovaty, EveryBlock is a website that filters an assortment of local news by location so you can keep track of what’s happening on your block, in your neighborhood and all over your city. The first release of EveryBlock contains information for Chicago, San Francisco, and New York. All written in Django.

Branching & Merging (4:10)

Matthew Wensing, co-founder of Stormpulse, joined us to talk about the GIS Branch, also known as GeoDjango.

Community Catchup (27:51)

  • Django People – Very cool site using google mashups to allow the Django community to get connected.
  • Per-Function Cache Decorator – Cool snippet that adds a decorator that caches the decorated function for the amount of time specified. Great for expensive operations.

Tip of the Week (43:40)

The templatetag {% block %} has a not so known variable }. It is documented, but it seems to be over looked. What this allows you to do is get the rendered output of the parent block and display it as you like. Take the following use case:

In a base.html you have the following bit of code:


{% block title %}My Cool Website{% endblock %}

Then in a template that extends the base.html template you want to be able to display “My Cool Website” as apart of each page:


{% extends "base.html" %}
{% block title %}My Sweet Page | {{ block.super }}{% endblock %}

The will display “My Sweet Page | My Cool Website”.

IRC Ad Nauseam (46:53)

Django IRC FAQ
Backwards Incompatible Changes Information

I just upgraded to the latest version and now I’m experiencing Unicode problems:

  • Read the Unicode Branch Checklist
  • Change the <i>str</i> methods on your models to be <i>unicode</i> methods.
  • Change string literals to be unicode strings. For example:

def __str__(self):
  return u'Category: %s' % (self.name,)

I keep getting this error “Reverse query name for field ’%s’ clashes with field ’%s.%s’. Add a related_name argument to the definition for ’%s’.” How do I correct it?

The error message is extremely helpful and tells you what you need to do. There are a lot of different ways this error will appear but usually the fix is the same. You need to specify a related_name argument when defining your field.

The problem often arises because you have two fields on a model that are a ForeignKey to the same table. For instance:


class Episode(models.Model):
    pub_date = models.DateTimeField()
    title = models.CharField(max_length=255)
    slug = models.SlugField(prepopulate_from=("title",), unique=True)
    categories = models.ManyToManyField(Category)
    body = models.TextField(blank=True)
    producer = models.ForeignKey(User, related_name='episode_producers')
    host = models.ForeignKey(User, related_name='episode_hosts')

u = User.objects.get(pk=1)
u.episode_producers
u.episode_hosts

Thank You! (55:31)

Comments - 4 people have already said something. Join the discussion.

  • Jökull said

    Another block trick is to do the following to your <ul> navigation elements:

    <ul id="{% block page %}{% endblock %}">
    <li class="front">Front</li>
    <li class="about">About</li>
    </ul>

    then in your css do:

    ul#front .front,
    ul#about .about { color: red; }

    Now in your templates you can extend the nav block with the name of the page. CSS will take care of highlighting it for you. Nice! Maybe not so Django related but a nice use of the Jinja template paradigm.

  • Jökull said

    Another block trick is to do the following to your &lt;ul&gt; navigation elements:

    &lt;ul id=\&quot;{% block page %}{% endblock %}\&quot;&gt;
    &lt;li class=\&quot;front\&quot;&gt;Front&lt;/li&gt;
    &lt;li class=\&quot;about\&quot;&gt;About&lt;/li&gt;
    &lt;/ul&gt;

    then in your css do:

    ul#front .front,
    ul#about .about { color: red; }

    Now in your templates you can extend the nav block with the name of the page. CSS will take care of highlighting it for you. Nice! Maybe not so Django related but a nice use of the Jinja template paradigm.

  • Anonymous said

    The __str__ method should not return Unicode. Replace __str__ with __unicode__, and in the base Model class there is a default __str__ the encodes the Unicode output.

  • Andreas said

    Very interesting with geodjango. It should be pretty clear to everyone that putting stuff on a map is a huge part of the new wave web, would be really awesome if the gis branch would become a offical part of django.