[Tutor] HTML form pre-populating functionality or hints on building my own

Magnus Lyckå magnus@thinkware.se
Thu Apr 17 19:58:01 2003


At Wed, 16 Apr 2003 19:39:11 -0700, Scott Chapman wrote:
>I'm wanting to find (or make) a module that will take a HTML file, locate the
>input-related form tags (<INPUT, <TEXTAREA, <SELECT, <OPTION) and modify them
>by putting in values from a dictionary (i.e. pre-populate the form). I want a
>module that is implemented 100% in python and does _not_ use any special tags
>to do this (such as webware, albatross, spyce, etc.)

I think my reply on "<021852.htm>extracting html <021852.htm>source" 
applies equally
well to you.

If you roll your own, use HTMLParser.HTMLParser, or perhaps
htmllib.HTMLParser if you don't need to support XHTML. Don't
use a general purpose text parser or RE.

Otherwise, if it's ok with you to put id attributes which
is completely kosher HTML and XHTML, but not typically
included in arbitrary HTML / XHTML files, use PyMeld.

With PyMeld, you will use completely clean HTML /XHTML that
will be edited and displayed correctly in whatever web
browser or editing software the HTML guys use.

You will need to have an id attribute in addition to the
name attribute though, and typically they will have the same
value. This is no big thing though, and it works for any
tag. You're not supposed to have name attributes in <P> or
<TD> tags for instance, so PyMeld is usable far beyond forms.

After all, the id attribute exists to provide a "document-wide
unique id" for tags, so if you want to be able to identify
individual tags, they *should* have an id attribute.

This id attribute might well be the key in your dict.