上下文处理器(context_processor钩子函数)
1、app.py
from flask import Flask, render_template, g
app = Flask(__name__)
@app.route('/')
def index():
return render_template('index.html')
@app.route('/article/')
def article():
return render_template('article.html')
@app.context_processor
def context_processor():
g.username = 'admin'
return {'username': g.username}
if __name__ == '__main__':
app.run()
2、index.html
{{ username }}
3、article.html
{{ username }}