- Flask MVC 架構的應用 -- 建立樣板目錄 templates
- 專案目錄結構:
flaskproject |- app |--- templates |------ index.html |--- __init__.py |--- router.py |main.py
Flask MVC 架構實作
- 將專案 flaskproject/app 目錄下,新增 templates 資料夾:
mkdir flaskproject/app/templates
- 將專案 flaskproject/app/templates 目錄下,新增 index.html 檔案:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> </head> <body> This is my web site!! </body> </html>
- 修改 flaskproject/app/router.py 檔案:
from flask import render_template from app import app @app.route("/") def index(): #return 'Hello World' return render_template('index.html')
- 重新啟動 Flask :
flask run
- 開啟瀏覽器,網址: http://127.0.0.1:5000