【发布时间】:2015-10-25 19:48:36
【问题描述】:
我正在学习 Python The Hard Way,我目前正在完成练习 51。在其中,要求学生在文件 templates/index.html 中添加一个链接 返回,以便我们可以继续填写表格并查看结果。我的代码如下:
/bin
app.py
/static
/templates
hello_form.html
index.html
/tests
app.py 的写法如下:
import web
urls = (
'/hello', 'Index'
)
app = web.application(urls, globals())
render = web.template.render('templates/', base="layout")
class Index(object):
def GET(self):
return render.hello_form()
def POST(self):
form = web.input(name="Nobody", greet="Hello")
greeting = "%s, %s" % (form.greet, form.name)
return render.index(greeting = greeting)
if __name__ == "__main__":
app.run()
index.html 写成如下:
$def with (greeting)
$if greeting:
I just wanted to say <em style="color: green; font-size: 2em;">$greeting</em>
$else:
<em>Hello</em>, world!
hello_form.html 写成如下:
<h1>Fill out this form</h1>
<form action="/hello" method="POST">
A Greeting: <input type="text" name="greet">
<br/>
Your Name: <input type="text" name="name">
<br/>
<input type="submit">
</form>
这个返回链接是表单上的按钮,不是吗? 如何为这个按钮添加处理程序?
提前感谢您的帮助。
【问题讨论】:
标签: python html post get web.py