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

时间:2014-12-09 12:56:08

标签: python html 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>

1 个答案:

答案 0 :(得分:2)

根据我的理解,你所需要的只是将数据保存到文件中,而你不知道如何在Flask中处理这个问题,我会尝试尽可能清楚地解释它:

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

# 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!'