Richard Jones' Log: Django peeves

Wed, 18 Jan 2006

I've been using Django for three days now. I'm getting more and more nervous about the black-magic model code. Today I ran into this ... strangeness. In my model code, I have:

class UTC(datetime.tzinfo):
    def utcoffset(self, dt):
        return datetime.timedelta(0)
    def tzname(self, dt):
        return "UTC"
    def dst(self, dt):
        return datetime.timedelta(0)
UTC = UTC()

class Challenge(meta.Model):
    .
    .
    .
    def start_utc(self):
        return datetime.datetime(self.start.year, self.start.month,
            self.start.day, 0, 0, 0, 0, UTC)

Looks reasonable enough, but it breaks. Says the global "UTC" isn't defined, inside start_utc(). I have to hard-code UTC=UTC in the method signature to make that code work. Update: this is fixed in 0.92, the next major release.

The hiding of exceptions in template rendering is really starting to annoy. That is, if there's an issue rendering {{ some_callable }} in a template, then I see nothing. I've even got DEBUG on. Nothing appears in the browser, the page source or the server log. It could be that I mis-typed "some_callabl" or perhaps "some_callable" is a call and that raised an exception. I won't know unless I manually wrap "some_callable" in a try/except/import traceback to find out!

Comment by jml on Wed, 18 Jan 2006

Supposedly there's a big refactoring coming in the next version:

http://code.djangoproject.com/wiki/RemovingTheMagic

Comment by Richard Jones on Wed, 18 Jan 2006

That's great to see! One of my peeves is addressed in a change that's already in :)

Comment by bitprophet on Wed, 18 Jan 2006

Actually, it's not broken -- look here, under 'module_constants'. It's certainly not intuitive nor is it well publicized, but it's there. But yes, magic-removal will be a nice, big welcome change, and hopefully it will be out soon.

Comment by bitprophet on Wed, 18 Jan 2006

Oh, and about the template stuff--there's also a TEMPLATE_DEBUG sister variable to vanilla DEBUG - if you set it to True in your settings.py file, you'll get nicely formatted tracebacks for template exceptions as well as everything else.

Comment by Richard Jones on Wed, 18 Jan 2006

Yep, discovered the module_constants setting after re-reading the doc.

TEMPLATE_DEBUG = DEBUG in my settings file, which is the default setting on a new app install. DEBUG is True, of course.

Comment by Tim Keating on Wed, 25 Jan 2006

Yes, TEMPLATE_DEBUG gives you nicely formatted tracebacks if an exception happens, but if it just can't resolve an attribute for some reason, you still get nothing. There's been some discussion of this on Django dev, and someone will probably submit a patch at some point . . .