基本情况: 最近折腾 Flask,学着做了一个小项目练手。会用简单的 html+css,不会 js
# app.py
import random
from flask import Flask, render_template
app = Flask(__name__)
### One douban film short comment ###
### random output ###
@app.route("/", methods=["GET"])
def index():
return render_template("index.html")
@app.route("/douban", methods=['GET'])
def Comment():
with open('douban.txt', 'r') as f:
lines = f.readlines()
num = random.choice(range(len(lines)))
return lines[num]
if __name__ == "__main__":
app.run()
模板文件夹 templates 下 index.html 文件内容如下:
My Flask$.get('/douban').done(function (data) {
var para=document.createElement("p");
var node=document.createTextNode(data);
para.appendChild(node);
var element=document.getElementById("comment");
element.appendChild(para);
});
现在可以做到的是,按 F5,首页会重新加载一条豆瓣电影短评,但这样不太方便。
所以我想如果在页面内添加一个刷新按钮,点击后 局部刷新