1.5.6 Creating Forms/Tables

Lets go back to a simple example:

import web.error; web.error.enable()
import web, web.database, web.database.object, os

connection = web.database.connect(type="sqlite", database="object-form.db")
cursor = connection.cursor() 

person = web.database.object.Table("Person")
person.addColumn(web.database.object.String(name="firstName", required=True))
person.addColumn(web.database.object.String(name="surname", required=True))

database = web.database.object.Database()
database.addTable(person)
database.init(cursor)

If we wanted to create a form to display as HTML to add a new person to the table we could use the following code:

>>> form = database['Person'].form()
>>> print form.html()
<form id="Person" class="pythonweb" action="" method="post" enctype="multipart/for
m-data">
<input type="hidden" name="table" value="Person">
<input type="hidden" name="mode" value="submitAdd">
<table border="0">
<tr><td><table border="0" cellpadding="3" cellspacing="0">
<tr>
  <td valign="top">Firstname </td>
  <td>&nbsp;&nbsp;&nbsp;</td>
  <td valign="top"><input type="text" name="Person.firstName" size="40" maxlengt
h="255" value=""></td>
  <td valign="top"></td>
</tr>
<tr>
  <td valign="top">Surname </td>
  <td>&nbsp;&nbsp;&nbsp;</td>
  <td valign="top"><input type="text" name="Person.surname" size="40" maxlength=
"255" value=""></td>
  <td valign="top"></td>
</tr>
</table>
</td></tr>
<tr><td>&nbsp;</td></tr>
<tr><td><input type="submit" value="Submit" name="action"></td></tr>
</table>
</form>

The form object generated by form = database['Person'].form() is a normal web.form.Form object and can be used exactly as any Form object can. See the documentation for the web.form module for more information.

Now we need to get the information the user enters into the database. As with all form objects we follow the following routine once we have a form object:

form = database['Person'].form() # Continuing from the previous example.
import web
print web.header()               # Print the content-type information
if len(web.cgi) > 1:             # Assume form submitted
    form.populate(web.cgi)
    if form.valid():
        entry = database['Person'].insert(all=form.dict())
        print '<html>%s<p><a href="%s">Go Back</a></html>'%(
            '<h1>Entry Added</h1>' + form.frozen(),
            os.environ['SCRIPT_NAME']
        )
    else:
        '<html><h1>Error</h1>%s</html>'%(
            """<p>There were some invalid fields. 
            Please correct them.</p>""" + form.html()
        )
else:
    entries = ''
    for row in database['Person'].values():
        entries += '%s %s<br />'%(row['firstName'] ,row['surname'])
    print "<html>%s<h4>Entries</h4><p>%s</p></html>"%(
        '<h1>Enter Data</h1>'+form.html(),
        entries
    )

And that's about it. We populate the form and check it is valid exactly as we would with any form object. The dictionary returned by form.dict() can be used in the database['Person'].insert() function by specifying it as the all parameter.

A handy point to note is that if you don't want the user to be able to add information to all of the form fields you can use the remove() method of the form to remove a field from the form by name before creating the HTML version of the form. For example:

>>> form = database['Person'].form() 
>>> form.remove('surname')
>>> print form.html()
<form id="Person" class="pythonweb" action="" method="post" enctype="multipart/for
m-data">
<table border="0">
<tr><td><table border="0" cellpadding="3" cellspacing="0">
<tr>
  <td valign="top">Firstname </td>
  <td>&nbsp;&nbsp;&nbsp;</td>
  <td valign="top"><input type="text" name="Person.firstName" size="40" maxlengt
h="255" value=""></td>
  <td valign="top"></td>
</tr>
</table>
</td></tr>
<tr><td>&nbsp;</td></tr>
<tr><td><input type="submit" value="Submit" name="action"></td></tr>
</table>
</form>



Subsections