form processing

125 views
Skip to first unread message

morecowbell

unread,
Jul 16, 2008, 1:15:31 AM7/16/08
to cherrypy-users
greetings, i'm web and cherry challenged and need what may be a minor
push to get going. i'm trying to finally abandon thick client design
(qt, c++) in favor of a restful approach. basically i got a battery of
checkboxes, select boxes, and other controls that make up a control
panel that "sockets" its way to a c++ app. i got most of the c++ app
ported to python already and was looking toward cherrypy for the web
server and tha'ts where things got a little tricky, two questions
right off the bat:

a) how do you get multiple values from the html form (post) into
cherrypy and
b) is there a best practice in gathering values from the "control
panel"? in qt it's easy to work of the changed values only in an event-
driven manner using signal/slots. in my new cherrypy world, it seems
like i need to process the set of values every time. please tell me
that i'm wrong.

thanks a bunch

Robert Brewer

unread,
Jul 16, 2008, 1:54:21 PM7/16/08
to cherryp...@googlegroups.com
morecowbell wrote:
> greetings, i'm web and cherry challenged and need what may be a minor
> push to get going. i'm trying to finally abandon thick client design
> (qt, c++) in favor of a restful approach. basically i got a battery of
> checkboxes, select boxes, and other controls that make up a control
> panel that "sockets" its way to a c++ app. i got most of the c++ app
> ported to python already and was looking toward cherrypy for the web
> server and tha'ts where things got a little tricky, two questions
> right off the bat:
>
> a) how do you get multiple values from the html form (post) into
> cherrypy and

Add function arguments. For example, if you have:

def thing(self):

...and your form sends "a=3&b=4&c=5", augment your function signature:

def thing(self, a, b, c=1):

In this example, a and b are required. c will default to 1 if not
supplied. Alternately, you can use keyword-args [1] to do the same
thing:

def thing(self, **kwargs):
a = kwargs['a']
b = kwargs['b']
c = kwargs.get('c', 1)

> b) is there a best practice in gathering values from the "control
> panel"? in qt it's easy to work of the changed values only in an
event-
> driven manner using signal/slots. in my new cherrypy world, it seems
> like i need to process the set of values every time. please tell me
> that i'm wrong.

There are three approaches:

1. Compare all the new values to the existing values. **kwargs can help
here:

for k, v in kwargs.iteritems():
if current[k] != v:
process_a_change(k, v)

2. Make a separate form (and cherrypy handler function) for each
control, or at least subsets of all controls.

3. Use AJAX [2] to send an individual message when each control
changes, rather than using a big 'submit' button. This will also imply a
separate cherrypy handler function for each control, but will not
require a complete refresh of the page for each request.

I would choose 3, most likely, but that will take more work on your part
if you're a web newbie.

Robert Brewer
fuma...@aminus.org

[1] http://docs.python.org/ref/calls.html#tok-keyword_arguments
[2] http://en.wikipedia.org/wiki/AJAX

Reply all
Reply to author
Forward
0 new messages