Jacob Kaplan-Moss

Tag: Forms

Dynamic form generation

I had the pleasure of being on a forms panel at PyCon 2010 chaired by Brandon Craig Rhodes. To get a stable baseline, Brandon asked each of us to provide code showing how each forms toolkit might tackle a problem:

Imagine that someone has already written a form with your forms library. The form looks something like this:

New username:    __________
Password:        __________
Repeat password: __________
    [Submit]

Now, someone from Marketing comes along and announces that the developers must add some additional questions to the form - and the number of extra questions is not determined until runtime! They give you a get_questions(request) function that looks up a profile they cook up for each person browsing the site, and returns a list of strings like one of these:

February 28th, 2010 • django forms