python flask学习记录

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

猜你喜欢

转载自www.cnblogs.com/spark-xl/p/9159610.html