2016-12-03 10 views
-1

我得到了我的頭圍繞python和運行它關閉服務器(在這種情況下瓶)。基本上我試圖打印本地文本文件的一些內容到一個HTML頁面。我從用戶那裏獲取一些輸入值(名稱,電子郵件和評論),然後將這些輸入值逐行存儲到本地文本文件中。目前,我可以在網頁上獲得最新的輸入內容(即評論,用戶的姓名和電子郵件),但每次表單重新提交時都會被替換。我現在只是搞亂了它(例如,我從文件讀取並打印5個用戶輸入實例到服務器)。不過,我試圖在form_action.html頁面上打印名稱,電子郵件和註釋,並保留所有以前的註釋。正如我所說我可以打印內容,並且它們顯示在cmd提示符中,但是可以通過javascript來完成,也許它會顯示在html頁面上,或者可以使用不同的方式使用python來完成。我正在使用的書不包括如何做到這一點,我也找不到任何網上的東西。任何人都能治好我的好奇心?如果我沒有理智,請問我,我會很樂意詳細說明。乾杯!是否可以通過javascript或python(Flask)將文本文件中的數據打印到html頁面?

app.py

from flask import Flask, render_template, request, url_for 

# Initialize the Flask application 
app = Flask(__name__) 

# Define a route for the default URL, which loads the form 
@app.route('/') 
def form(): 
return render_template('form_submit.html') 

# Define a route for the action of the form, for example '/hello/' 
# We are also defining which type of requests this route is 
# accepting: POST requests in this case 
@app.route('/hello/', methods=['POST']) 
def hello(): 
    name=request.form['yourname'] 
    email=request.form['youremail'] 
    comment=request.form['yourcomment'] 


    f = open ("user+comments.txt","a") 

    f.write(name) 
    f.write(' ') 
    f.write(email) 
    f.write(' ') 
    f.write(comment) 
    f.write('\n') 

    f.close() 

    #This reads the the first 5 lines and prints them to the cmd prompt 
    with open("user+comments.txt") as f: 
     i = 1 
     for x in range (0,5): 
     lines = f.readlines(i) 
     print(lines) 
     i+=1 
     x+=1 
    f.close() 




return render_template('form_action.html', name=name, email=email, 
comment=comment) 

# Run the app :) 
if __name__ == '__main__': 
app.run(debug=True) 

form_submit.html

// This page takes in the initial information via the text boxes and  
    passes the information to the python file above 

<html> 
<body style="background-color: #3DC247;"> 

<head> 
<title>Python</title> 
</head> 
<body> 

    <div align = "center"> 
    <h1 style="font-family:verdana;">PYTHON PAGE</h1> 

    <body> 
    <div id="container"> 
     <div class="title"> 
      <h3 style="font-family:verdana;">Please fill in your details  
    below and your comment to join the discussion</h3> 
     </div> 
     <div id="content"> 
      <form method="post" action="{{ url_for('hello') }}"> 
       <label for="yourname" style="font-family:verdana;">Please 
    enter your name:</label> 
       <input type="text" name="yourname" /><br /><br> 

       <label for="youremail" style="font-family:verdana;">Please 
    enter your email:</label> 
       <input type="text" name="youremail" /><br /><br> 

       <label for="yourcomment"style="font-family:verdana;">Please 
    enter your comment:</label> 
       <input type="textarea" name="yourcomment" rows="4" cols="50">  
    <br> 
       <input type="submit" /><br> 
      </form> 
     </div> 

    </body> 
    </html> 

form_action.html

// I'm trying to get the information to pass to this page. The text boxes  
    from the previous html page remain on this html page as I want to    
    continue to add comments without having to go back to form_submit.html  
    each time 

    <html> 

    <body style="background-color: #3DC247;"> 

    <head> 
    <title>Python</title> 
    </head> 

    <div align = "center"> 

    <h1 style="font-family:verdana;">PYTHON PAGE</h1> 


    <body> 
    <div id="container"> 
     <div class="title"> 
      <h3 style="font-family:verdana;">Please fill in your details 
    below and your comment to join the discussion</h3> 
     </div> 
     <div id="content"> 
      <form method="post" action="{{ url_for('hello') }}"> 
       <label for="yourname" style="font-family:verdana;">Please 
    enter your name:</label> 
       <input type="text" name="yourname" /><br /><br> 

       <label for="youremail" style="font-family:verdana;">Please 
    enter your email:</label> 
       <input type="text" name="youremail" /><br /><br> 

       <label for="yourcomment"style="font-family:verdana;">Please 
    enter your comment:</label> 
       <input type="textarea" name="yourcomment" rows="4" cols="50">  
    <br> 
       <input type="submit" /><br> 
      </form> 
    // I included this bit as an original attempt to post a single comment  
     to the page(it will display while still passing the info to the text 
     file via python) 
    <body> 
    <div id="container"> 
     <div class="title"> 
      <p></p> 
     </div> 
     <div id="content"> 
      <strong>{{name}} ({{email}}):</strong><br> 
      {{comment}} 
     </div> 
     </div> 
     </body> 
    </html> 
+0

這是什麼意思?你想達到什麼目的? – Nicarus

+1

本質上,我試圖實現的目標是能夠基於用戶輸入到文本字段(名稱,電子郵件,評論)中的內容在網頁上顯示一些評論,類似於非常基本的排序論壇。所以基本上,例如,joe bloggs輸入他的詳細信息之後; 「Joe Bloggs([email protected])我愛Python」,將會顯示。接下來,Jane Doe可能會出現並添加一條可以閱讀的評論; 「Jane Doe([email protected])我也愛Python」,將會顯示。這是否更有意義? – Charles

+0

如通常所示,如果您需要多個元素,則必須創建包含元素的列表。然後將此列表發送到模板並使用模板'for'功能顯示此列表中的元素。 – furas

回答

0

您正在讀取和寫入文件確定,但在模板中,您只獲取姓氏或條目,因爲您沒有存儲從文件中讀取的內容名單。 試一下在這一個辦法:

f = open("user+comments.txt", "a") 

f.write(name + ':' + email + ':' + comment) 
f.write('\n') 
f.close() 

details = [] 
# This reads the the first 5 lines and prints them to the cmd prompt 
with open("user+comments.txt") as f: 
    i = 1 
    for x in range(0, 5): 
     lines = f.readlines(i) 
     print(lines) 
     i += 1 
     x += 1 
     try: 
      details.append(lines[0].split(':')) 
     except: 
      'List cannot be populated' 
f.close() 

return render_template('form_action.html', details=details) # return details list containing user, email, comment 

我加入「:」如姓名,電子郵件和評論之間的分離器,所以它更容易分裂條目

「詳細資料」退換現在包含一切所需爲模板。

但在模板中,你需要一個循環中也是一樣,瓶這是這樣的:

{% for user, email, comment in details %} 
         User: {{ user }} <br/> 
         Email: {{ email }} <br/> 
         Comment: {{ comment }} <br/> 
        {% endfor %} 

and the screenshot from your html output

你需要扭轉的條目爲好,這樣你就可以顯示最後5個, 希望它有幫助!

+1

這就是我要找的謝謝你!任何想法,爲什麼它可能導致以下錯誤,雖然:ValueError:沒有足夠的值解壓(預期3,得到1) – Charles

+0

嘗試'解包'只有一個,看看它是什麼,所以而不是名稱,電子郵件,提交只使用名稱爲循環。當您從列表中請求比實際存在的更多元素時,會發生此錯誤。 – Deckey

1

在你的GET路線 「(」/」 )「您需要閱讀文件中的所有註釋,然後執行render_template並將其傳遞給form_submit模板。

在form_submit模板中,您將循環瀏覽這些註釋,並在該循環中呈現每個html的html。

我並不那麼熟悉燒瓶如何做,但這似乎是模板化Web應用程序中最合乎邏輯的方式。

這裏實際上並不需要兩個模板。在POST路由('/ hello')中,您應該將其重定向回根(「/」),並讓它再次處理渲染。

此外,我會建議不必爲不同的路線,基本上,相同的資源。 「/」可以接受最初的GET,然後POST。但只在最後做到這一點...

相關問題