Flask 的新手并且有一些使用 python 的经验,当使用 render_template 时,它不会渲染模板,也不会给出任何错误。 代码在这里:
from flask import Flask, render_template
app = Flask(__name__, template_folder= "/templates")
@app.route("/")
def index():
#return("Index str")
return render_template("index.html")
@app.route("/crawler")
def crawler():
return("WebCrawler str")
return render_template("crawler.html")
if __name__ == "__main__":
app.run()
app.debug = True
此处为 HTML(相当确定的文件层次结构是正确的)。
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" type="text/css" href="{{url_for('static', filename='style.css')}}">
<meta charset = "utf-8">
<title> Index</title>
<head>
<body>
<p> This webserver hosts all webapps and will showcase any unfinished applications.
</p>
</body>
</html>
最佳答案
HTML 文件的默认文件夹是模板。因此,在您的 python 文件所在的位置创建一个名为“templates”的文件夹。将任何 HTML 文件放入该文件夹
关于html - Flask 渲染模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44607950/