在 LeanCloud 雲引擎部署 Flask 應用時,訪問根路徑 / 返回「The requested URL was not found on the server」,但本地開發環境完全正常。
LeanCloud 雲引擎要求入口文件為 wsgi.py,且必須暴露 application 物件。常見錯誤:
project_root/
├── wsgi.py # 雲引擎入口
├── app/
│ ├── __init__.py # 建立 Flask app 並註冊 Blueprint
│ ├── home/
│ │ ├── __init__.py
│ │ ├── views.py # 定義 home_view Blueprint
│ │ └── templates/
│ │ └── login.html
│ └── static/
└── requirements.txt
app/__init__.py
from flask import Flask
def create_app():
app = Flask(__name__)
from .home.views import home_view
app.register_blueprint(home_view) # 關鍵:註冊 Blueprint
return app
wsgi.py
from app import create_app
application = create_app() # LeanCloud 尋找的變數名必須是 application
app/home/views.py
from flask import Blueprint, render_template
home_view = Blueprint('home', __name__, template_folder='templates')
@home_view.route('/', methods=['GET', 'POST'])
def homep():
return render_template('login.html')
wsgi.py 在專案根目錄application 變數指向 Flask app 實例template_folder='templates'requirements.txt 包含 flask、leancloud-sdkgunicorn wsgi:application 測試通過LeanCloud 建議靜態文件走 CDN,若必須由 Flask 服務,需在創建 app 時指定:
app = Flask(__name__, static_folder='../static', static_url_path='/static')