【问题标题】:Getting input from a browser - python + forms从浏览器获取输入 - python + forms
【发布时间】:2017-09-09 04:56:26
【问题描述】:

完成 LPTHW 练习 51 并碰壁。尝试通过浏览器获取一些基本的用户输入,然后显示它。代码如下——第一个python:

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>

当我点击“提交”时,我得到的唯一结果是“未找到”。

【问题讨论】:

  • 您的表单 action 属性说它会到达 /hello 端点,但我没有在 Python 代码的任何地方看到它

标签: python html forms python-2.7


【解决方案1】:

您在 Python 文件的代码中遗漏了一些重要内容。

你应该这样写:

urls = (
   '/hello', 'Index'

而不是这个:

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

【讨论】:

    猜你喜欢
    • 2014-01-13
    • 1970-01-01
    • 2014-12-18
    • 2015-08-09
    • 2017-09-13
    • 2014-03-24
    • 2015-03-07
    • 1970-01-01
    相关资源
    最近更新 更多