我是一个初学者,请考虑一下,因此,我需要传递页脚和日期函数,并且在某种程度上我想忽略这里的要点
from flask import Flask, render_template
from datetime import datetime
app = Flask("Timer Workout")
@app.route("/")
def Landing():
return render_template("Landing_Page.html")
def footer():
footerdb = open("footer.txt")
for i in range (3):
footerdb.write("footer.txt" + " by Carlos ")
footerdb.close()
return render_template("Landing_Page.html", footerdb)
同样,在这里,我无法返回日期函数,我不知道该怎么办。帮助表示赞赏。非常感谢。
@app.route("/index.html/")
def Home():
current_time = datetime.datetime.now()
return render_template("index.html", current_time = current_time)
if "Timer Workout" == "__main__":
app.run()
该函数未返回任何内容,因为未调用它。
您希望将日期/时间返回哪里?
如果在/index.html中,则应执行以下操作:
@app.route("/index.html/")
def Home():
current_time = datetime.datetime.now()
return render_template("index.html", current_time=current_time)
然后在您的模板中,您可以在HTML代码中添加变量,例如:
Current Date and Time: {{ current_time }}}
基本上,您可以在python主脚本中定义变量值。将它们作为参数传递给render_template函数,然后通过{{ your_var }}
在模板内部使用它们。
在这里看看:https://jinja.palletsprojects.com/en/2.11.x/templates/