0

I have a small .py program, rendering 2 HTML pages. One of those HTML pages has a form in it. A basic form requesting a name, and a comment. I can not figure out how to take the name and the comment from the form and store it into the csv file. I have got the coding so that the very little I already manually input into the csv file is printed/returned on the HTML page, which is one of the goals. But I can't get the data I input into the form into the csv file, then back n the HTML page. I feel like this is a simple fix, but the Flask book makes absolutely no sense to me, I'm dyslexic and I find it impossible to make sense of the examples and the written explanations.

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>
4
  • form action method should be 'post'. and your guestbook() displays only the page. No code to handle the incoming data and store them in csv file. Commented Dec 9, 2014 at 13:09
  • Can you add the code of your saveComment() function? Commented Dec 9, 2014 at 13:15
  • I know I need another @app.route (?) to handle the data and store it, but everywhere I look, even on here all the suggestions I come across are conflicting and don't make sense to me. Commented Dec 9, 2014 at 13:17
  • I've tried writing a writer=csv.writer as another page suggest but then my HTML pages would display because the coding wasn't right. Commented Dec 9, 2014 at 13:18

1 Answer 1

5

By what I understand all you need is to save the data into the file and you don't know how to handle this in Flask, I'll try to explain it with code as clear as possible:

# 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!'
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.