用 Jinja2 渲染页面并管理静态资源。

本节要点

  • render_template 渲染 Jinja2 模板
  • 模板放在 templates 目录
  • 静态文件放在 static 目录

代码示例

from flask import render_template

@app.route('/hello/<name>')
def hello(name):
    return render_template('hello.html', name=name)
提示:模板中默认开启自动转义,输出 HTML 需谨慎使用 safe。