In the previous section we saw how web applications can capture user input from browser requests. This section explains how Albatross <al-input> tags can be used to take values from the execution context and format them as value attributes in the HTML <input> tags sent to the browser.
The sample program from this section is supplied in the samples/templates/form2 directory and can be installed in your web server cgi-bin directory by running the following commands.
cd samples/templates/form2 python install.py
The first change is in the form.html template file.
<html> <head> <title>Display Form Input</title> </head> <body> Input some values to the following form and press the submit button. <form method="post" action="form.py"> Text field: <al-input name="text"><br> Singleton checkbox: <al-input type="checkbox" name="singleton"><br> Checkbox group: <al-input type="checkbox" name="group" value="check1"> <al-input type="checkbox" name="group" value="check2"><br> Radio buttons: <al-input type="radio" name="radio" value="radio1"> <al-input type="radio" name="radio" value="radio2"><br> Option menu: <al-select name="select"> <al-option>option1</al-option> <al-option>option2</al-option> <al-option>option3</al-option> </al-select> <al-input type="submit" name="submit" value="Submit"> </form> <al-include name="form-display.html"> </body> </html>
We need to place some values into the execution context so that the Albatross <al-input> tags can display them. The easiest thing to do is to place the browser submitted values into the execution context.
The documentation for the Python cgi module is quite good so I will not try to explain the complete behaviour of the FieldStorage class. The only behaviour that we need to be aware of for our program is what it does when it receives more than one value for the same field name.
The FieldStorage object that captures browser requests behaves like a dictionary that is indexed by field name. When the browser sends a single value for a field, the dictionary lookup yields an object containing the field value in the value member. When the browser sends more than one value for a field, the dictionary lookup returns a list of the objects used to represent a single field value.
Using this knowledge, the form.py program can be modified to merge the browser request into the execution context.
#!/usr/bin/python import cgi from albatross import SimpleContext form = cgi.FieldStorage() ctx = SimpleContext('.') ctx.locals.form = form for name in form.keys(): if type(form[name]) is type([]): value = [] for elem in form[name]: value.append(elem.value) else: value = form[name].value setattr(ctx.locals, name, value) templ = ctx.load_template('form.html') templ.to_html(ctx) print 'Content-Type: text/html' print ctx.flush_content()
You can see the program output by pointing your browser at http://www.object-craft.com.au/cgi-bin/alsamp/form2/form.py.
You will notice that your input is sent back to you as the default value of each form element.
When you use Albatross application objects the browser request is automatically merged into the execution context for you.