Posts Tagged ‘beertraq’

Unicode in Django, It’ll Getcha

Posted in Uncategorized on February 11th, 2010 by Nick – Be the first to comment

I’ve been having an insane time trying to squash a weird unicode bug in Beertraq. This was one of the first bugs that was found after starting the beta and it’s taken me this long to get it worked out. It all started with a cryptic-seeming error upon viewing the add-a-beer page:

UnicodeEncodeError: ‘ascii’ codec can’t encode characters in position 0-9: ordinal not in range(128)

Real descriptive, right? Looking through the stack trace wasn’t much help either. It was a bunch of stuff pertaining to trying to render the template. One thing I did notice is that a brewer with a unicode name had recently gotten added and this error was only happening on the new beer form (which includes a ChoiceField of brewers), but the brewer was being displayed just fine on its own page.

After asking my friend and fellow Django-user Dan-o what he thought, he asked if I had __str__() and __unicode__() defined in my models. I had __str__() defined (generally a good idea) but not __unicode__(). I popped it in to my Brewer model as so:

class Brewer(models.Model):
  TYPE_CHOICES = (
    ('Macrobrewer', 'Macrobrewer'),
    ('Microbrewer', 'Microbrewer/Craft brewer'),
    ('Homebrewer', 'Homebrewer'),
    ('Unknown', 'Unknown'),
  )

  name = models.CharField("Brewer name", max_length=255)
  brewer_type = models.CharField("Type of brewer", max_length=25, choices=TYPE_CHOICES)
  city = models.CharField("Brewer's city", max_length=255, blank=True)
  approved = models.BooleanField(default=False)

  def __str__(self):
    return self.name

  def __unicode__(self):
    return self.name

  class Meta:
    ordering = ['name']

And guess what? My problems went away. I feel like an idiot for being foiled by something so simple that I overlooked. It makes sense too because __str__() is used quite a bit in forms where the model instance’s name needs to be magically generated. Without __unicode__() it would cause Django to barf on unicode names, just like it did on me.

Let this be a lesson to you. If you might EVER have ANY unicode text in your model instance’s names, for crap’s sake define __unicode__()!!

Beertraq Beta!

Posted in Uncategorized on December 5th, 2009 by Nick – Be the first to comment

I’m glad to announce that my recent pet project, Beertraq, is now in a (somewhat closed) beta stage! The basic idea is there and functioning, but the extra functionality isn’t done and it’s far from polished. Nonetheless, it’s time to take her for a test drive!

So what is Beertraq, you ask? It’s a way for you to keep track of which beers you’ve tasted, compare those with others, read beer reviews, and most importantly discover new beers to try. I originally got the idea from The Flying Saucer’s UFO Club, where members work toward a goal of drinking 200 different beers. Once they complete the task, they get their name on a plate which gets put on the wall of the bar. The cool part about the UFO Club is that it’s all computerized, using a magstripe card to login at a kiosk in the bar. You can also log in to their website to check your progress and read reviews on there. I figured that if The Flying Saucer can have that system for their bar, I could do the same for the world.

If you’re interested in becoming a BeerTraq beta user, send an email to beertraq (at) beertraq (dot) com with the email address you want to use for your account. All I ask is that you give feedback by filling out issue requests with bugs you find or suggestions you might have.