python Flask學習
安裝環境
這里我開始用的是flask中文文檔中的virtualenv,搭建好運行后發現報錯訪問不了
(venv) sp4rk@sp4rk-HP-Notebook:~/pypractice$ python3 test.py
* Serving Flask app "test" (lazy loading)
* Environment: production
WARNING: Do not use the development server in a production environment.
Use a production WSGI server instead.
* Debug mode: on
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
* Restarting with stat
* Debugger is active!
* Debugger PIN: 808-715-409
127.0.0.1 - - [08/Jun/2018 20:19:31] "GET / HTTP/1.1" 404 -
這里老是報錯,后來檢查了一下代碼,發現少寫了個@app.root('/')
hello word
from flask import Flask
app = Flask(__name__)
@app.root('/')
def Hello_word():
return "Hello word!"
@app.route('/test')
def test():
return "test route"
if __name__ == "__main__":
app.run()
#調試 app.run(debug=True)
#其他公網訪問 用 app.run(host='0.0.0.0')
路由
剛才那里少寫了,發現是路由,可以把函數綁定到URL上面
用method=['GET', 'POST']指定接受方式
url_for 構造路徑
給靜態文件生成url url_for('static', filename="style.css")