Skip to main content

Django

I had promised to develop an application to manage the local sailplane club. For a long time I kept postponing in, but over the last Christmas vacation, I just tackled the problem. I decided I would finally sit down and learn the Django framework enough to be able to develop such an application (which in itself is pretty simple: track flights, transactions, expenses and do some pdf reporting).

The learning experience with django was a very good one: the documentation is extremely well done and easily searchable, and you can find a lot of examples all over to find some inspiration. My only small stumbling block was the integration of the Dojo Toolkit (even choosing one was not so simple given how many there are) with the newforms library. The issue was made more acute by the Firefox bug triggered by Dojo. In any case here's a small excerpt to get Dojo's Time Dijit widget working with Django forms:

class MyTimeInput(django_widgets.Input):
    input_type = 'text'
     def render(self, name, value, attrs=None):
        value = ''
        if isinstance(value, time):
            value = "T%s" % value.replace(microsecond=0)

        return super(MyTimeInput, self).render(name, value, attrs)

class TimeField(django_fields.TimeField):
    def __init__(self, *args, **kwargs):
        post_on_change = kwargs.pop('post_on_change', False)
        if not kwargs.has_key('widget'):
            kwargs.update({'widget':MyTimeInput(attrs={
               'dojoType':'dijit.form.TimeTextBox',
               'constraints':"{timePattern:'HH:mm'}"})})
        self.input_formats = ('%H:%M:%S', '%H:%M',)
        super(django_fields.TimeField, self).__init__(*args, **kwargs)
        if post_on_change:
            self.widget.attrs['onChange'] = "_set(this.id,
                arguments[0])"

    def clean(self, value):
        return super(TimeField, self).clean(value[1:])