あなたはdecrators
を使用することができます。 Pythonのデコレータについての記事を読むここでhere
は以下flask.Theコードでカスタムデコレータのデモンストレーションでご利用の場合のデコレータの定義と使用方法を示し
コード
from flask import Flask,request
from functools import wraps
def popup_message(f):
@wraps(f)
def f_(*args,**argv):
message_read = request.args.get('message_read', None)
if message_read is not None:
return message_read
else:
return f(*args,**argv)
return f_
app = Flask(__name__)
@app.route('/earth')
@popup_message
def hello_earth():
return 'Hello,earth'
@app.route('/world')
@popup_message
def hello_world():
return 'Hello, World!'
app.run()
使用
アプリを実行する
python main.py
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
と/ earthと/ worldとのやりとりを依頼してください。message_read
回答を確認して、コメントや意見を投稿してください –