基本上,我们的想法是将注释存储在csv文件中并将它们显示在同一页面上。问题是我想把时间大胆,但我找不到在哪里做的位置。我认为主要问题出在HTML文件中。
from flask import Flask, redirect,render_template,request
import csv
from time import gmtime, strftime
app = Flask(__name__)
@app.route('/')
def home():
return render_template('home.html')
def readFile(aFile):
#read a file and return a list
with open(aFile, 'r') as inFile:
reader = csv.reader(inFile)
aList = [row for row in reader] #I think the problem is here
return aList
def writeFile(aList, aFile):
#write a list to file
with open(aFile, 'w', newline='') as outFile:
writer = csv.writer(outFile)
print(aList)
writer.writerows(aList)
return
@app.route('/comments')
def comments():
#read the contacts list from file
fileName = 'static\\comments.csv'
commentList = readFile(fileName)
return render_template('comments.html',commentList=commentList)
@app.route('/addComment', methods = ['POST'])
def addComment():
#read the contacts list from file
fileName = 'static\\comments.csv'
commentList = readFile(fileName)
# add an entry to the skills list
name = request.form[('name')]
date = strftime("%a, %d %b %Y %X", gmtime())
#adds the current date and time
comment = request.form[('comment')]
newComment=[name,date,comment]
commentList.append(newComment)
#save the skills list to the file
writeFile(commentList, fileName)
return redirect('comments')

<ul class="comments">
{% for comments in commentList %}<br>
<b><li>{% for names in comments %}</li> <!-- I think the problem is here-->
<li>{{names}}</li></b>
{% endfor %}
{% endfor %}
</ul>
&#13;
答案 0 :(得分:0)
找到解决方案。列表应如下所示:
<ul class="comments">
{% for line in commentList %}
<li> <b>{{ line[0] }}</b> <br> <b>{{ line[1] }}</b> <br> {{ line[2] }} </li><br>
{% endfor %}
</ul>
&#13;
答案 1 :(得分:0)
<ol>
<li>Coffee</li>
<li>Tea</li>
<li>Milk</li>
</ol>
<ul>
<li>Coffee</li>
<li>Tea</li>
<li>Milk</li>
</ul>
对于<li>
... </li>
行的中断,br-tags很好并且可以使用。 <li>
... <br>
... <br>
... </li>
。 br标签是单一的。来自W3Schools的HTML <br> Tag。
<li> ... <br> ... <br> ... </li>
永远记住规则:在HTML内容中更好地坚持[a-z] [0-9]。因此,在测试中没有花括号和百分比符号。