使用Flask和Python检索HTML表单数据并存储在csv中

0 html python forms csv flask

我有一个小的.py程序,呈现2个HTML页面。这些HTML页面之一包含一个表单。请求名称和注释的基本表单。我不知道如何从表单中获取名称和注释并将其存储到csv文件中。我已经进行了编码,因此我已经手动输入到csv文件中的很少的内容将在HTML页面上打印/返回,这是目标之一。但是我无法将输入表单的数据输入到csv文件中,然后返回HTML页面。我觉得这是一个简单的解决方法,但是Flask这本书对我来说绝对没有意义,我患有阅读障碍,而且我无法理解这些示例和书面说明。

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>
Run Code Online (Sandbox Code Playgroud)

Lea*_*let 5

据我了解,您所需要的只是将数据保存到文件中,而且您不知道如何在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!'
Run Code Online (Sandbox Code Playgroud)