CherryPy - select checkboxes for selecting variables

I am trying to create a simple webpage with a few checkboxes, a text box and a buttom submit.

I just came across Python web programming and am trying to figure out how to do this with CherryPy.

I need to associate each checkbox with a variable so that my .py file knows which ones were selected when the Start button was clicked. Can someone please provide an example code? Do I have any advantages, including a Python Javascript compiler like Pajamas?

<form action="../remote_targets/ssh_grab.py"> <label for="goal"><strong>Host Availability:</strong></label> <input style="margin-left: 30px;" type="checkbox" name="goal[]" value="cpu" /> CPU idle<br> <input style="margin-left: 30px;" type="checkbox" name="goal[]" value="lighttpd" /> Lighttpd Service<br> <input style="margin-left: 30px;" type="checkbox" name="goal[]" value="mysql" /> Mysql Service<br> </form> 

Thanks!

+4
source share
1 answer

Here's a minimal example:

 import cherrypy class Root(object): @cherrypy.expose def default(self, **kwargs): print kwargs return '''<form action="" method="POST"> Host Availability: <input type="checkbox" name="goal" value="cpu" /> CPU idle <input type="checkbox" name="goal" value="lighttpd" /> Lighttpd Service <input type="checkbox" name="goal" value="mysql" /> Mysql Service <input type="submit"> </form>''' cherrypy.quickstart(Root()) 

And here is the terminal output:

 $ python stacktest.py [10/Sep/2010:14:25:55] HTTP Serving HTTP on http://0.0.0.0:8080/ CherryPy Checker: The Application mounted at '' has an empty config. Submitted goal argument: None 127.0.0.1 - - [10/Sep/2010:14:26:09] "GET / HTTP/1.1" 200 276 "" "Mozilla..." Submitted goal argument: ['cpu', 'mysql'] 127.0.0.1 - - [10/Sep/2010:14:26:15] "POST / HTTP/1.1" 200 276 "http://localhost:8003/" "Mozilla..." [10/Sep/2010:14:26:26] ENGINE <Ctrl-C> hit: shutting down app engine [10/Sep/2010:14:26:26] HTTP HTTP Server shut down [10/Sep/2010:14:26:26] ENGINE CherryPy shut down $ 

As you can see, CherryPy will collect multiple controls with the same name in the list. You do not need the suffix [] to inform him of this. Then go through the list to see what values ​​were sent. (Keep in mind that if only one item is selected, the goal argument will be a single line instead of a list!)

+9
source

All Articles