CherryPy - 保存复选框选择到变量 [英] CherryPy - saving checkboxes selection to variables
问题描述
我试图建立一个简单的网页,其中包含多个复选框,一个Textbox和一个提交Buttom。
I'm trying to build a simple webpage with multiple checkboxes, a Textbox and a submit buttom.
我刚刚碰到了Python的网络编程试图找出来做与CherryPy。
I've just bumped into web programing in Python and am trying to figure out out to do it with CherryPy.
我需要关联每个复选框到一个变量,所以我的.py文件知道哪些被选中点击开始按钮。
有人可以给一些代码示例吗?
我有任何优势,包括像睡衣的Python JavaScript编译器吗?
I need to associate each checkbox to a variable so my .py file knows which ones were selected when clicking the 'Start button'. Can someone please give some code example ? Do I have any advantage including some Python Javascript Compiler like Pyjamas?
<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>
$ b
谢谢!
Thanks !
推荐答案
这是一个最小的例子:
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())
这里是终端输出:
$ 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
$
如您所见,CherryPy将在列表中收集多个具有相同名称的控件。你不需要 []
后缀来告诉它。然后,遍历列表以查看提交的值。 (请记住,如果只选择了一个项目,则 goal
参数将是单个字符串,而不是列表!)
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!)
这篇关于CherryPy - 保存复选框选择到变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!