【问题标题】:Retrieving HTML form data and storing in csv with Flask & Python使用 Flask 和 Python 检索 HTML 表单数据并存储在 csv 中
【发布时间】:2015-02-07 09:03:06
【问题描述】:

我有一个小的 .py 程序,渲染 2 个 HTML 页面。其中一个 HTML 页面中有一个表单。要求姓名和评论的基本表格。我不知道如何从表单中获取名称和评论并将其存储到 csv 文件中。我已经获得了编码,以便在 HTML 页面上打印/返回我已经手动输入到 csv 文件中的非常少的内容,这是目标之一。但是我无法将输入到表单中的数据放入 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>

【问题讨论】:

  • 表单操作方法应该是'post'。并且您的 guestbook() 仅显示页面。没有代码来处理传入的数据并将它们存储在 csv 文件中。
  • 你能添加你的saveComment()函数的代码吗?
  • 我知道我需要另一个 @app.route (?) 来处理数据并存储它,但是无论我看哪里,即使在这里,我遇到的所有建议都是相互矛盾的,没有意义对我来说。
  • 我已经尝试按照另一个页面的建议编写 writer=csv.writer,但是由于编码不正确,我的 HTML 页面会显示出来。

标签: python html forms csv flask


【解决方案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!'

【讨论】:

    猜你喜欢
    • 2021-11-05
    • 2014-08-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-30
    • 2011-04-30
    • 1970-01-01
    相关资源
    最近更新 更多