flask學習之我的第一個flask頁面<一>
阿新 • • 發佈:2020-07-31
首先裝好python pycharm,然後再裝上flask ,pip install flask即可
先新建一個專案:flaskone
在flaskone下新建python檔案:
from flask import Flask,render_template app = Flask(__name__) #使用當前模組 @app.route('/hello/<name>') def hello_world(name): #引數名與url的引數名一致,都必須是name return render_template('hello.html',names=name) #前一個引數必須與模板頁面中的引數名一致為names if __name__ == '__main__': app.run(debug=True) #開啟除錯模式,run方法用來啟動服務
在flaskone下新建templates資料夾,在下面新建hello.html網頁 (templates資料夾與python檔案應該在同一層級才可以)
<!doctype html> <html> <body> <h1>大家好 我叫 {{ names }}!</h1> </body> </html>
啟動該指令碼之後,
瀏覽器輸入http://127.0.0.1:5000/hello/emily,結果展示如下