Python HTML将表单数据写入文件

Python HTML将表单数据写入文件,python,html,forms,input,Python,Html,Forms,Input,尝试编写一个简单的应用程序: 提供一个html表单以获取输入数据,包括问候语和姓名 在浏览器中显示数据,然后这是我无法理解的部分 将输入表单数据保存到简单的.txt文件中 以下是python代码: import web urls = ( '/hello', 'Index' ) app = web.application(urls, globals()) render = web.template.render('templates/', base='layout') class

尝试编写一个简单的应用程序:

提供一个html表单以获取输入数据,包括问候语和姓名

在浏览器中显示数据,然后这是我无法理解的部分

将输入表单数据保存到简单的.txt文件中

以下是python代码:

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() # returns form to browser

    def POST(self): # gets name and greeting data back
        form = web.input(name="Nobody", greet="Hello")
        greeting = "%s ... %s" % (form.greet, form.name)
        return render.index(greeting = greeting) #displays index.html
        with open ('projects/forms/ninja.txt)', 'w') as out_file:
        out_file.write(form.getValue("greet"))
        out_file.write(form.getValue("name"))

if __name__ == "__main__":
    app.run()
以下是hmtl表格:

<h1>this is hello_form</h1>

<form action="/hello" method="POST">
    A Greeting: <br>
    <input type="text" name="greet">
    <br/>
    Your Name: <br>
    <input type="text" name="name">
    <br/>
   <input type="submit">
</form>
我搜索了其他答案,包括这一个:


但是我似乎找不到信息来进行这项工作。

您正在用这行代码对/hello执行POST请求

<form action="/hello">
但是,您尚未为此路由定义控制器。结果,什么也没发生

在/Index上发表文章或为/hello创建处理程序


此外,我强烈建议为此制定一个框架。一些小东西,比如。

同意。有些框架使简单的事情变得更加困难,但Flask确实很容易使用。它们显然已经在使用一个框架web.py,并且同样清楚地将/hello路由设置为指向索引处理程序。在return语句之后,您无法执行任何操作。