2014-12-09 94 views
0

我有一個小的.py程序,呈現2個HTML頁面。其中一個HTML頁面中有一個表單。一個請求姓名的基本表單和一條評論。我無法弄清楚如何從窗體中獲取名稱和註釋並將其存儲到csv文件中。我已經得到了編碼,以便將已經手動輸入到csv文件中的很少的內容打印/返回到HTML頁面上,這是目標之一。但是我無法將輸入到表單中的數據輸入到csv文件中,然後返回到HTML頁面。我覺得這是一個簡單的解決方法,但Flask的書對我來說絕對沒有意義,我有閱讀障礙,我發現不可能理解這些例子和書面解釋。檢索HTML表單數據並使用Flask&Python存儲在csv中

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> 
+0

表單操作方法應該是'post'。和你的留言板()只顯示該頁面。沒有代碼來處理傳入的數據並將它們存儲在csv文件中。 – chfw 2014-12-09 13:09:54

+0

你可以添加saveComment()函數的代碼嗎? – 2014-12-09 13:15:28

+0

我知道我需要另一個@ app.route(?)來處理數據並存儲它,但是在我看來,即使在這裏,我所遇到的所有建議都是相互衝突的,對我來說沒有意義。 – Thebonebed 2014-12-09 13:17:54

回答

2

通過我瞭解你需要的是將數據保存到文件中,你不知道如何在瓶處理這個問題,我會嘗試用代碼儘可能清楚地解釋:

# 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!' 
相關問題