1
2 """
3 Tutorial 03 - Passing variables
4
5 本教程展示如何处理 GET/POST 变量在函式中
6 - 使用GBK 省了麻烦
7 - 注意编码时的缩进!一定要使用空格来代替缩进符号
8 - POST 方式可以隐藏参数的传送
9 - GET 方式在调试时好用!
10
11 """
12 from cherrypy import cpg
13
14
15 class WelcomePage:
16
17 def index(self):
18
19 return '''
20 <form action="greetUser" method="POST">
21 What is your name?
22 <input type="text" name="name" />
23 <input type="submit" />
24 </form>
25 '''
26
27 index.exposed = True
28
29 def greetUser(self, name = None):
30 """CherryPy 处理GET/POST变量为函式的参数,对于Python 没有什么限制,比如说长度什么的
31 - 当然我们可以自由的定义变量,只要和表单中的对象名一致就好!
32 - 注意的是,若 某一表单项没有值返回,会默许的赋为 None ,这样一来我们可以将其作为条件来判定
33 """
34 if name:
35
36 return "Hey %s, what's up? 怎么了?哈哈哈!" % name
37 else:
38
39 return 'Please enter your name <a href="./">here</a>.'
40
41 greetUser.exposed = True
42
43
44 cpg.root = WelcomePage()
45 cpg.server.start(configFile = 'tutorial.conf')
CheerPyTut/03_get_and_post.py (last edited 2009-12-25 07:13:04 by localhost)