flask模版中使用全局变量

发布时间 2023-11-01 23:19:25作者: 风吹南下

 

from flask import Flask

app = Flask(__name__)

@app.context_processor
def inject_global_variables():
    return {'site_name': 'My Website'}

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

 

模版中使用site_name全局变量

<!DOCTYPE html>
<html>
<head>
    <title>{{ site_name }}</title>
</head>
<body>
    {% block content %}
    {% endblock %}
</body>
</html>