python flask學習記錄

python Flask學習

安裝環境

這裏我開始用的是flask中文文檔中的virtualenv,搭建好運行後發現報錯訪問不了css

(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('/')python

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")flask

相關文章
相關標籤/搜索