CherryPy - saving checkboxes selection to variables

后端 未结 1 1164
小鲜肉
小鲜肉 2021-01-13 16:36

I\'m trying to build a simple webpage with multiple checkboxes, a Textbox and a submit buttom.

I\'ve just bumped into web programing in Python and am trying to figur

1条回答
  •  庸人自扰
    2021-01-13 17:23

    Here's a minimal example:

    import cherrypy
    
    class Root(object):
        @cherrypy.expose
        def default(self, **kwargs):
            print kwargs
            return '''
    Host Availability: CPU idle Lighttpd Service Mysql Service
    ''' 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  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 into a list. You don't need the [] suffix to tell it to do that. Then, iterate over the list to see which values were submitted. (Keep in mind that, if only one item is selected, then the goal argument will be a single string instead of a list!)

    0 讨论(0)
提交回复
热议问题