2017-07-30 98 views
0

通过LPTHW练习51练习并打墙。尝试通过浏览器获得一些基本的用户输入,然后显示它。代码如下 - 第一个Python:从浏览器获取输入信息 - python + forms

import web 

urls = (
    '/', 'Index' 
) 

app = web.application(urls, globals()) 

render = web.template.render('templates/') 

    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() 

然后HTML:

<html> 
    <head> 
     <title>Sample web form</title> 
    </head> 
<body> 

<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> 

</body> 
</html> 

当我点击“提交”等我回来的仅仅是“未找到”。

+0

你的形式'action'属性表示,它打算到'/ hello'点,但我没有看到它指定在Python代码中的任何地方 – Mangohero1

回答

0

你错过了Python代码中重要的东西。

你应该这样写:

urls = (
    '/hello', 'Index' 

,而不是这样的:

urls = (
    '/', 'Index' ...