python flask 调用html模板
时间: 2023-06-01 12:03:23 浏览: 115
可以使用Flask框架中的render_template()函数来调用HTML模板。具体步骤如下:
1. 在Flask应用程序中导入render_template()函数:
```
from flask import Flask, render_template
```
2. 在应用程序中创建一个路由,使用render_template()函数来渲染HTML模板:
```
@app.route('/hello')
def hello():
return render_template('hello.html')
```
在上面的例子中,/hello是一个路由,当用户访问该路由时,会调用hello()函数,并返回渲染后的hello.html模板。
3. 在Flask应用程序中创建一个templates文件夹,将HTML模板放在该文件夹中。
注意:Flask框架默认会在应用程序根目录下的templates文件夹中查找HTML模板。
示例代码:
```
from flask import Flask, render_template
app = Flask(__name__)
@app.route('/hello')
def hello():
return render_template('hello.html')
if __name__ == '__main__':
app.run(debug=True)
```
在上面的代码中,我们创建了一个Flask应用程序,并创建了一个/hello路由,当用户访问该路由时,会调用hello()函数,并返回渲染后的hello.html模板。如果需要在开发环境下调试应用程序,可以将debug参数设置为True,这样就可以看到详细的调试信息。
阅读全文
相关推荐
















