2017-07-30 97 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' ...