我無法弄清楚 Blueprint 類的問題
我決定使用 Blueprint 類,我投入了這段代碼
from flask import Flask, Blueprint
app = Flask(__name__)
admin = Blueprint('admin',__name__,template_folder='templates')
app.register_blueprint(admin, url_prefix='/admin')
@admin.route('/',methods=('GET', 'POST'))
def index():
return 'admin'
if __name__ == "__main__":
app.run(debug=True)
但是地址不存在,啟動時終端顯示這個
UserWarning: The setup method 'route' can no longer be called on the blueprint 'admin'. It has already been registered at least once, any changes will not be applied consistently.
Make sure all imports, decorators, functions, etc. needed to set up the blueprint are done before registering it.
This warning will become an exception in Flask 2.3.
@admin.route('/',methods=('GET', 'POST'))
/Users/a-v-tor/Documents/flaskproj/.venv/lib/python3.10/site-packages/flask/scaffold.py:449: UserWarning: The setup method 'add_url_rule' can no longer be called on the blueprint 'admin'. It has already been registered at least once, any changes will not be applied consistently.
Make sure all imports, decorators, functions, etc. needed to set up the blueprint are done before registering it.
This warning will become an exception in Flask 2.3.
self.add_url_rule(rule, endpoint, f, **options)
/Users/a-v-tor/Documents/flaskproj/.venv/lib/python3.10/site-packages/flask/blueprints.py:490: UserWarning: The setup method 'record' can no longer be called on the blueprint 'admin'. It has already been registered at least once, any changes will not be applied consistently.
Make sure all imports, decorators, functions, etc. needed to set up the blueprint are done before registering it.
This warning will become an exception in Flask 2.3.
self.record(
根據碼頭,理論上,我有一個作業版本......我嘗試通過 add_url_rule 沒有裝飾器,但都是徒勞的,我不知道該怎么辦。燒瓶版本 2.2.2
uj5u.com熱心網友回復:
您的應用正在多次注冊藍圖。這是您的解決方法:
from flask import Flask, Blueprint
admin = Blueprint('admin',__name__,template_folder='templates')
def create_app():
app = Flask(__name__)
app.register_blueprint(admin)
return app
@admin.route('/',methods=('GET', 'POST'))
def index():
return 'admin'
if __name__ == "__main__":
app = create_app()
app.run(debug=True)
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/532811.html
標籤:Python烧瓶
