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>
表單操作方法應該是'post'。和你的留言板()只顯示該頁面。沒有代碼來處理傳入的數據並將它們存儲在csv文件中。 – chfw 2014-12-09 13:09:54
你可以添加saveComment()函數的代碼嗎? – 2014-12-09 13:15:28
我知道我需要另一個@ app.route(?)來處理數據並存儲它,但是在我看來,即使在這裏,我所遇到的所有建議都是相互衝突的,對我來說沒有意義。 – Thebonebed 2014-12-09 13:17:54