The screen to add a new book to the database is a simple form. What we need to implement is:
Some HTML to make it possible to display the form
A method in our CherryPy application that will produce this HTML
A method to process the input once this form is submitted
There is no need to implement two different methods here because based on the arguments passed to the method we can decide whether to return a form or to process the submitted contents of the same form. Although it may be considered bad form to design a method to do two things, it does keep related functionality together.
The addbookform
class variable contains the template that refers to a number of string variables to interpolate. There is also a <script>
element to add some extra functionality that we examine later:
Chapter5/booksweb.py
addbookform='''<div id="newbook"> <form action="addbook" method="get"> <fieldset><legend>Add new book</legend...