我得到了我的頭圍繞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>
這是什麼意思?你想達到什麼目的? – Nicarus
本質上,我試圖實現的目標是能夠基於用戶輸入到文本字段(名稱,電子郵件,評論)中的內容在網頁上顯示一些評論,類似於非常基本的排序論壇。所以基本上,例如,joe bloggs輸入他的詳細信息之後; 「Joe Bloggs([email protected])我愛Python」,將會顯示。接下來,Jane Doe可能會出現並添加一條可以閱讀的評論; 「Jane Doe([email protected])我也愛Python」,將會顯示。這是否更有意義? – Charles
如通常所示,如果您需要多個元素,則必須創建包含元素的列表。然後將此列表發送到模板並使用模板'for'功能顯示此列表中的元素。 – furas