使用Flask&;检索HTML表单数据并存储在csv中;python

使用Flask&;检索HTML表单数据并存储在csv中;python,python,html,forms,csv,flask,Python,Html,Forms,Csv,Flask,我有一个小的.py程序,呈现2个HTML页面。其中一个HTML页面中有一个表单。请求名称和注释的基本表单。我不知道如何从表单中获取名称和注释并将其存储到csv文件中。我已经得到了编码,所以我已经手动输入到csv文件中的很少内容会打印/返回到HTML页面上,这是目标之一。但我无法将输入表单的数据输入csv文件,然后返回HTML页面。我觉得这是一个简单的解决办法,但《烧瓶》这本书对我来说毫无意义,我患有诵读困难症,我发现不可能理解这些例子和书面解释 This is the code I have f

我有一个小的.py程序,呈现2个HTML页面。其中一个HTML页面中有一个表单。请求名称和注释的基本表单。我不知道如何从表单中获取名称和注释并将其存储到csv文件中。我已经得到了编码,所以我已经手动输入到csv文件中的很少内容会打印/返回到HTML页面上,这是目标之一。但我无法将输入表单的数据输入csv文件,然后返回HTML页面。我觉得这是一个简单的解决办法,但《烧瓶》这本书对我来说毫无意义,我患有诵读困难症,我发现不可能理解这些例子和书面解释

This is the code I have for reading the csv back onto the page;

   @app.route('/guestbook')
        def guestbook(): 
            with open('nameList.csv','r') as inFile:
                reader=csv.reader(inFile)
                names=[row for row in reader]
            return render_template('guestbook.html',names=names[1:])


And this is my form coding;
    <h3 class="tab">Feel free to enter your comments below</h3>
            <br />
            <br />
            <form action="" method="get" enctype="text/plain" name="Comments Form">
            <input id="namebox" type="text" maxlength="45" size="32" placeholder="Name"
            class="tab"/>
            <br />
            <textarea id="txt1" class="textbox tab" rows="6" placeholder="Your comment"
            class="tab" cols="28"></textarea>
            <br />
            <button class="menuitem tab" onclick="clearComment()" class="tab">Clear
            comment</button>
            <button class="menuitem" onclick="saveComment()" class="tab">Save comment</button>
            <br>
            </div>
这是我用来将csv读回页面的代码;
@应用程序路径(“/guestbook”)
def guestbook():
以open('nameList.csv','r')作为填充:
reader=csv.reader(infle)
名称=[读卡器中的行对行]
返回呈现模板('guestbook.html',names=names[1:])
这是我的表单编码;
请随意在下面输入您的评论




清楚的 评论 保存评论

据我所知,您所需要的只是将数据保存到文件中,而您不知道如何在Flask中处理此问题,我将尝试用代码尽可能清楚地解释:

# request is a part of Flask's HTTP requests
from flask import request
import csv

# methods is an array that's used in Flask which requests' methods are
# allowed to be performed in this route.
@app.route('/save-comment', methods=['POST'])
def save_comment():
    # This is to make sure the HTTP method is POST and not any other
    if request.method == 'POST':
        # request.form is a dictionary that contains the form sent through
        # the HTTP request. This work by getting the name="xxx" attribute of
        # the html form field. So, if you want to get the name, your input
        # should be something like this: <input type="text" name="name" />.
        name = request.form['name']
        comment = request.form['comment']

        # This array is the fields your csv file has and in the following code
        # you'll see how it will be used. Change it to your actual csv's fields.
        fieldnames = ['name', 'comment']

        # We repeat the same step as the reading, but with "w" to indicate
        # the file is going to be written.
        with open('nameList.csv','w') as inFile:
            # DictWriter will help you write the file easily by treating the
            # csv as a python's class and will allow you to work with
            # dictionaries instead of having to add the csv manually.
            writer = csv.DictWriter(inFile, fieldnames=fieldnames)

            # writerow() will write a row in your csv file
            writer.writerow({'name': name, 'comment': comment})

        # And you return a text or a template, but if you don't return anything
        # this code will never work.
        return 'Thanks for your input!'
#请求是Flask HTTP请求的一部分
从烧瓶进口请求
导入csv
#方法是在Flask中使用的数组,请求的方法是
#允许在此路线中执行。
@app.route('/save comment',methods=['POST'])
def save_comment():
#这是为了确保HTTP方法是POST而不是任何其他方法
如果request.method==“POST”:
#form是一个包含通过发送的表单的字典
#HTTP请求。此操作通过获取的name=“xxx”属性来完成
#html表单字段。因此,如果您想获取名称,请输入
#应该是这样的:。
name=请求。表单['name']
comment=请求。表单['comment']
#此数组是csv文件中的字段,在以下代码中
#您将看到如何使用它。将其更改为实际的csv字段。
FieldName=['name','comment']
#我们重复与读数相同的步骤,但用“w”表示
#文件将被写入。
打开('nameList.csv','w')作为填充:
#DictWriter将通过处理
#csv作为python的类,将允许您使用
#字典,而不必手动添加csv。
writer=csv.DictWriter(填充,字段名=字段名)
#writerow()将在csv文件中写入一行
writer.writerow({'name':name,'comment':comment})
#返回文本或模板,但如果不返回任何内容
#这个代码永远不会工作。
返回“感谢您的输入!”

据我所知,您所需要的只是将数据保存到文件中,而您不知道如何在Flask中处理此问题,我将尝试用代码尽可能清楚地解释:

# request is a part of Flask's HTTP requests
from flask import request
import csv

# methods is an array that's used in Flask which requests' methods are
# allowed to be performed in this route.
@app.route('/save-comment', methods=['POST'])
def save_comment():
    # This is to make sure the HTTP method is POST and not any other
    if request.method == 'POST':
        # request.form is a dictionary that contains the form sent through
        # the HTTP request. This work by getting the name="xxx" attribute of
        # the html form field. So, if you want to get the name, your input
        # should be something like this: <input type="text" name="name" />.
        name = request.form['name']
        comment = request.form['comment']

        # This array is the fields your csv file has and in the following code
        # you'll see how it will be used. Change it to your actual csv's fields.
        fieldnames = ['name', 'comment']

        # We repeat the same step as the reading, but with "w" to indicate
        # the file is going to be written.
        with open('nameList.csv','w') as inFile:
            # DictWriter will help you write the file easily by treating the
            # csv as a python's class and will allow you to work with
            # dictionaries instead of having to add the csv manually.
            writer = csv.DictWriter(inFile, fieldnames=fieldnames)

            # writerow() will write a row in your csv file
            writer.writerow({'name': name, 'comment': comment})

        # And you return a text or a template, but if you don't return anything
        # this code will never work.
        return 'Thanks for your input!'
#请求是Flask HTTP请求的一部分
从烧瓶进口请求
导入csv
#方法是在Flask中使用的数组,请求的方法是
#允许在此路线中执行。
@app.route('/save comment',methods=['POST'])
def save_comment():
#这是为了确保HTTP方法是POST而不是任何其他方法
如果request.method==“POST”:
#form是一个包含通过发送的表单的字典
#HTTP请求。此操作通过获取的name=“xxx”属性来完成
#html表单字段。因此,如果您想获取名称,请输入
#应该是这样的:。
name=请求。表单['name']
comment=请求。表单['comment']
#此数组是csv文件中的字段,在以下代码中
#您将看到如何使用它。将其更改为实际的csv字段。
FieldName=['name','comment']
#我们重复与读数相同的步骤,但用“w”表示
#文件将被写入。
打开('nameList.csv','w')作为填充:
#DictWriter将通过处理
#csv作为python的类,将允许您使用
#字典,而不必手动添加csv。
writer=csv.DictWriter(填充,字段名=字段名)
#writerow()将在csv文件中写入一行
writer.writerow({'name':name,'comment':comment})
#返回文本或模板,但如果不返回任何内容
#这个代码永远不会工作。
返回“感谢您的输入!”

表单操作方法应为“post”。并且您的留言簿()只显示页面。没有处理传入数据并将其存储在csv文件中的代码。您可以添加saveComment()函数的代码吗?我知道我需要另一个@app.route(?)来处理数据并将其存储,但无论我到哪里,即使在这里,我遇到的所有建议都是相互冲突的,对我来说没有意义。我曾尝试按照另一个页面的建议编写一个writer=csv.writer,但我的HTML页面会显示出来,因为编码不正确。表单操作方法应该是“post”。并且您的留言簿()只显示页面。没有处理传入数据并将其存储在csv文件中的代码。您可以添加saveComment()函数的代码吗?我知道我需要另一个@app.route(?)来处理数据并将其存储,但无论我到哪里,即使在这里,我遇到的所有建议都是相互矛盾的,对我来说没有意义