如何使用Flask框架打开包含框架的HTML

0 投票
1 回答
1025 浏览
提问于 2025-04-17 23:46

我想打开index.html,

但是找不到pic.html。

我该怎么做呢?

pic.html和index.html都在templates文件夹里。

index.html的内容是:

  <body>
    <iframe src="pic.html"></iframe>
  </body>

flaskdemo.py的内容是:

from flask import Flask,render_template

app = Flask(__name__)


@app.route('/')
def hello_world():
    return render_template("index.html")

if __name__ == '__main__':
    app.run()

1 个回答

1

Flask这个框架不会直接从它的templates文件夹里提供文件。如果你想要使用/pic.html这个页面,你需要为它设置一个路由,并在这个路由中渲染相应的模板:

from flask import Flask,render_template

app = Flask(__name__)


@app.route('/')
def hello_world():
    return render_template("index.html")

@app.route('/pic.html')
def pic():
    return render_template("pic.html")

if __name__ == '__main__':
    app.run()

撰写回答