Django Forms, I’m Dumping You

Dear Django Forms,

We’ve had a tough relationship and I just don’t think it’s working out. I’ve tried to be patient while you work things out. I know that you’ve been trying to change. You’ve cleaned up your act and dropped the manipulation. But really, your new widgets just aren’t doing anything for me. I need a little onfocus and onclick, you know? And sometimes I need you to be able to validate my complex requests.

Maybe I’m giving up on you too soon, but I can’t always be telling you the things you could be doing better. Sometimes it’s just easier to do it myself.

I’ll miss you,
Leah

P.S. I just thought you should know that I’ve been seeing some raw XHTML lately.

8 Comments

  1. Posted April 23, 2007 at 2:01 pm | Permalink

    Wait, you were dating Django Forms too!?! That two-timing… Now I know leaving them was a good idea.

  2. Posted April 23, 2007 at 3:36 pm | Permalink

    ouch!

    It is funny that your kiss off, was over the head of more than half the world. I had to search to find what Django Forms even where.

  3. Posted April 23, 2007 at 9:27 pm | Permalink

    I haven’t tried this, but it seems that form fields can override the widgets associated with them… and you can add attributes when creating the widgets.

    OK, I just tried it. Take a look:

    >>> import django.newforms as forms
    >>> class cf(forms.Form):
    … name = forms.CharField(widget=forms.TextInput(attrs={”foo”:”bar”}))

    >>> f = cf()
    >>> print f
    Name:
    >>>

    Note the extra foo=”bar” attribute. Neat, eh?

  4. Posted April 23, 2007 at 9:32 pm | Permalink

    My last post has HTML that the comment poster seems to have automatically stripped out, so you’ll have to try this on the command-line yourself to see what output is produced. I assure you that it’s pretty cool.

    Here it is with some “” characters replaced with { and }.

    >>> import django.newforms as forms
    >>> class cf(forms.Form):
    … name = forms.CharField(widget=forms.TextInput(attrs={”foo”:”bar”}))

    >>> f = cf()
    >>> print f
    {tr}{th}{label for=”id_name”}Name:{/label}{/th}{td}{input foo=”bar” type=”text” name=”name” id=”id_name” /}{/td}{/tr}
    >>>

    How silly-looking. But you see what I’m getting at. Set attrs={”onclick”:”a whole bunch of javascript”} and go to town.

  5. Posted April 23, 2007 at 10:05 pm | Permalink

    Hahahaha… I think I’m going to go you’re route too with ClassOwl, I need a lot more granular control over my forms.

  6. sandro

    Posted April 25, 2007 at 7:17 pm | Permalink

    Richard, great find indeed, looks like an excellent way to add attributes to form elements.

    Leah, I hate to be such a standards nerd but onclick definitely isn’t raw XHTML, let’s get some unobtrusive javascript in there before XHTML gets jealous!

  7. Posted April 26, 2007 at 10:36 am | Permalink

    sandro and richard - those attributes are stipped of code, so you can’t set script attributes that way. Another option is to set the listeners on the HTML objects using javascript after the HTML objects are created.

  8. sandro

    Posted April 26, 2007 at 6:13 pm | Permalink

    Yes, unobtrusive javascript / dom scripting 4tw!