第一:创建虚拟环境,安装flask包
新建项目包 mkdir watchlist
python3 创建虚拟环境 python -m venv env
激活虚拟环境 env/Scripts/activate.bat
安装flask包 pip install flask
临时调用国内源安装方法
在安装时带上参数 -i 国内源地址
例:pip install pytest -i https://pypi.tuna.tsinghua.edu.cn/simple/
附国内源:
清华:https://pypi.tuna.tsinghua.edu.cn/simple
阿里云:https://mirrors.aliyun.com/pypi/simple/
中国科技大学 https://pypi.mirrors.ustc.edu.cn/simple/
华中理工大学:http://pypi.hustunique.com/
山东理工大学:http://pypi.sdutlinux.org/
豆瓣:https://pypi.douban.com/simple/
第二:创建app.py
新建python文件,命名为app.py,默认项目入口文件
from flask import Flask from flask import escape,url_for app=Flask(__name__) @app.route('/') def hello(): return "Welcome to My Watchlist!"
运行项目 flask run
(env) $ flask run * Serving Flask app "app.py" * Environment: production WARNING: Do not use the development server in a production environment. Use a production WSGI server instead. * Debug mode: off * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
值的获取escape
@app.route('/user/<name>') def user_page(name): return 'User: %s'% escape(name)
要使用 Flask 提供的 escape() 函数对 name 变量进行转义处理,比如把 < 转换成 <。这样在返回响应时浏览器就不会把它们当做代码执行。
第三:配置环境变量,开启调试模式
安装管理环境变量的包 python-dotenv
pip install python-dotenv
创建.flaskenv文件,开启调试模式
FLASK_ENV=development
注意,要正常启动app,app.py需要注明
app = Flask(__name__)
或者
FLASK_APP=app.py
FLASK_ENV=development
第四:url_for的使用
@app.route('/test') def test_url_for(): #下面是一些调用示例(请在命令行窗口查看输出的 URL): print(url_for('hello')) #输出:/ #注意下面两个调用是如何生成包含 url 变量的 URL 的 print(url_for('user_page',name='huguo002')) #输出:/user/huguo002 print(url_for('user_page',name='peter')) #输出:/user/peter print(url_for('test_url_for')) #输出:/test #下面这个调用传入了多余的关键字参数,它们会被作为信号线字符串附加到 URL 后面。 print(url_for('test_url_for',num=2)) #输出 /test?num=2 return 'Test page'
第五:模板的使用,index.html
创建模板文件 mkdir templates 同一级目录
与django一样,使用Jinja2
{{ ... }} 用来标记变量。
{% ... %} 用来标记语句,比如 if 语句,for 语句等。
{# ... #} 用来写注释。
注释方法
{# #}
过滤器
{{ 变量|过滤器 }}
迭代的使用
{% for movie in movies %} {# 迭代 movies 变量 #} <li>{{ movie.title }} - {{ movie.year }}</li> {# 等同于 movie['title'] #} {% endfor %} {# 使用 endfor 标签结束 for 语句 #}
创建首页,index.html
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <link rel="icon" href="{{ url_for('static',filename='favicon.ico')}}"> <title>{{ name }}'s Watchlist</title> </head> <body> <h2> <img alt="Avatar" src="{{ url_for('static',filename='images/avatar.png' )}}"> {{ name }}'s Watchlist' </h2> {# 使用 length 过滤器来获取 movies 变量的长度 #} <p>{{ movies|length }} Titles</p> <ul> {% for movie in movies %} {# 迭代 movies 变量 #} <li>{{ movie.title }} - {{ movie.year }}</li> {#等同于 movie['title']#} {% endfor %} {# 使用 endfor 标签结束 for 语句 #} </ul> <footer> <small>©2020 HelloFlask</small> </footer> </body> </html>
准备虚拟数据
name = 'huguo00289' movies = [ {'title': 'My Neighbor Totoro', 'year': '1988'}, {'title': 'Dead Poets Society', 'year': '1989'}, {'title': 'A Perfect World', 'year': '1993'}, {'title': 'Leon', 'year': '1994'}, {'title': 'Mahjong', 'year': '1996'}, {'title': 'Swallowtail Butterfly', 'year': '1996'}, {'title': 'King of Comedy', 'year': '1999'}, {'title': 'Devils on the Doorstep', 'year': '1999'}, {'title': 'WALL-E', 'year': '2008'}, {'title': 'The Pork of Music', 'year': '2012'}, ]
渲染主页模板
from flask import Flask, render_template # ... @app.route('/') def index(): return render_template('index.html', name=name, movies=movies)
来源:Flask 入门教程-李辉
https://read.helloflask.com/
感谢作者的优质入门教程
相比django,flask的确够简洁,如果与django对比,你会发现flask想要实现helloword的话,省去了很多步骤,当然作为都是热门的 python web 框架,两者还是存在联系和相同之处的,比如Jinja2语法!