当前个人开发环境是Miniconda3+PyCharm。开发环境其实无所谓,本身使用Python3+Nodepad均可以。安装Flask库:html
pip install Flask
将如下内容保存为helloworld.py:spring
# 导入Flask类 from flask import Flask # 实例化,可视为固定格式 app = Flask(__name__) # route()方法用于设定路由;相似spring路由配置 @app.route('/') def hello_world(): return 'Hello, World!' if __name__ == '__main__': # app.run(host, port, debug, options) # 默认值:host=127.0.0.1, port=5000, debug=false app.run()
直接运行该文件,而后访问:http://127.0.0.1:5000/helloworld。结果以下图:sql
Flask默认到templates目录下查找模板文件,在上边htlloworld.py同级目录下建立templates文件夹。数据库
在templates文件夹下建立get.html,写入如下内容:django
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>get请求示例</title> </head> <body> <form action="/deal_request" method="get"> <input type="text" name="q" /> <input type="submit" value="搜索" /> </form> </body> </html>
再在templates文件夹下建立post.html,写入如下内容:json
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>post请求示例</title> </head> <body> <form action="/deal_request" method="post"> <input type="text" name="q" /> <input type="submit" value="搜索" /> </form> </body> </html>
最后在templates文件夹下建立result.html,写入如下内容:flask
<!-- Flask 使用Jinja2模板引擎,Jinja2模板引擎源于Django板模因此不少语法和Django是相似的 --> <h1>{{ result }}</h1>
在helloworld.py中添加get_html()、post_html()和deal_request()三个方法,更多说明见注释。当前helloworld.py内容以下:api
# 导入Flask类 from flask import Flask from flask import render_template from flask import request # 实例化,可视为固定格式 app = Flask(__name__) # route()方法用于设定路由;相似spring路由配置 #等价于在方法后写:app.add_url_rule('/', 'helloworld', hello_world) @app.route('/helloworld') def hello_world(): return 'Hello, World!' # 配置路由,当请求get.html时交由get_html()处理 @app.route('/get.html') def get_html(): # 使用render_template()方法重定向到templates文件夹下查找get.html文件 return render_template('get.html') # 配置路由,当请求post.html时交由post_html()处理 @app.route('/post.html') def post_html(): # 使用render_template()方法重定向到templates文件夹下查找post.html文件 return render_template('post.html') # 配置路由,当请求deal_request时交由deal_request()处理 # 默认处理get请求,咱们经过methods参数指明也处理post请求 # 固然还能够直接指定methods = ['POST']只处理post请求, 这样下面就不须要if了 @app.route('/deal_request', methods = ['GET', 'POST']) def deal_request(): if request.method == "GET": # get经过request.args.get("param_name","")形式获取参数值 get_q = request.args.get("q","") return render_template("result.html", result=get_q) elif request.method == "POST": # post经过request.form["param_name"]形式获取参数值 post_q = request.form["q"] return render_template("result.html", result=post_q) if __name__ == '__main__': # app.run(host, port, debug, options) # 默认值:host=127.0.0.1, port=5000, debug=false app.run()
从新运行helloworld.py。restful
当前目录结构以下(.idea目录不用管):app
get.html以下:
get查询结果以下:
post.html以下:
post查询结果以下:
restful我这里经过Flask-RESTful实现。
pip install flask-restful
修改helloworld.py,添加三处,具体见注释
# 导入Flask类 from flask import Flask from flask import render_template from flask import request # 添加位置一。从Flask-RESTful中导入如下三项 from flask_restful import Api from flask_restful import Resource from flask_restful import reqparse # 实例化,可视为固定格式 app = Flask(__name__) # 添加位置二。 # 实例化一个api用于配置rest路由 # 实例化一个参数解析类,用于rest获取get和post提交的参数 api = Api(app) parser = reqparse.RequestParser() # 注册q参数parser才能解析get和post中的q参数。这种注册才能解析的要求是否有点孤儿 parser.add_argument('q', type=str, help='Rate to charge for this resource') # route()方法用于设定路由;相似spring路由配置 #等价于在方法后写:app.add_url_rule('/', 'helloworld', hello_world) @app.route('/helloworld') def hello_world(): return 'Hello, World!' # 配置路由,当请求get.html时交由get_html()处理 @app.route('/get.html') def get_html(): # 使用render_template()方法重定向到templates文件夹下查找get.html文件 return render_template('get.html') # 配置路由,当请求post.html时交由post_html()处理 @app.route('/post.html') def post_html(): # 使用render_template()方法重定向到templates文件夹下查找post.html文件 return render_template('post.html') # 配置路由,当请求deal_request时交由deal_request()处理 # 默认处理get请求,咱们经过methods参数指明也处理post请求 # 固然还能够直接指定methods = ['POST']只处理post请求, 这样下面就不须要if了 @app.route('/deal_request', methods = ['GET', 'POST']) def deal_request(): if request.method == "GET": # get经过request.args.get("param_name","")形式获取参数值 get_q = request.args.get("q","") return render_template("result.html", result=get_q) elif request.method == "POST": # post经过request.form["param_name"]形式获取参数值 post_q = request.form["q"] return render_template("result.html", result=post_q) # 添加位置三。 # 添加rest类(是类而不是和Flask同样直接是方法) class Rest(Resource): # get提交时的处理方法 def get(self): result = {} args = parser.parse_args() result["method"] = "get" result["q"] = args["q"] return result # post提交时的处理方法 def post(self): result = {} # 此种方法便可解析经过普通post提交也可解析json格式提交 args = parser.parse_args() result["method"] = "post" result["q"] = args["q"] return result # 配置路由 api.add_resource(Rest, '/rest') if __name__ == '__main__': # app.run(host, port, debug, options) # 默认值:host=127.0.0.1, port=5000, debug=false app.run()
即然是restful咱们也就不写html了,直接用curl模拟提交。从新运行helloworld.py,请求以下图(以前非rest的get和post固然也还能正常访问):
咱们常常会据说这样的一个观点:Django是Python最流行的Web框架但配置比较复杂,Flask是一个轻量级的框架配置比较简单若是项目比较小推荐使用Flask。
若是对Django不是很了解,能够参看“Python3+PyCharm+Django+Django REST framework开发教程”和“Python3+Django get/post请求实现教程 ”。
仅从文章长度看就比这篇长不少,因此Django比Flask复杂(得多)是确定的。更具体比较以下:
比较项 | Django | Flask | 复杂度比较 | 说明 |
项目建立 | Django须要用命令建立项目 | Flask直接编写文件就可运行 | Django复杂 | Django须要用命令建立项目是由于要建立出整个项目框架 |
路由 | Django使用专门的urls.py文件 | Flask直接使用@app.route() | Django笨重 | Django相似Strut2的配置Flask相似Spring的配置,Flask感受更好 |
get和post | request.GET['name']和request.POST["name"] | request.args.get("name","")和request.form["q"] | 差很少 | Flask格式上不统一 |
restful | 使用django-resful框架 | 使用flask-restful框架 | 差很少 | Flask使用方法flask-rest使用类格式不统一;flask-restful须要注册参数有点孤儿 |
数据库操做 | django集成了对数据库的操做 | Flask没集成对数据库的操做要另行直连或使用sqlalchemy | 差很少 | django复杂很大程度来源于对数据库的集成。 |
Django复杂来源于其“可能用到的功能都先集成”,Flask的轻量来源其“暂时不用的功能都先不作处理”。随着项目规模的扩大最终Django有的东西Flask也都须要有,并且因为没作统一规划Flask将处于劣势位置。
我的认为Flask适用如下两个场景:
一是开发者没有Spring等企业级框架开发经验,否则Django不少项为何要配置为何这样配置不那样配置比较难理解。
二是项目大概是十来个页面、就只有两三张数据表甚至不用数据库的规模,在这种程度下减小的配置工做还算能抵得过增长的开发工做量。
若是以前用过Spring之类的框架写过代码的话我的仍是推荐无论项目大小都直接用Django,主要是Flask不统一的格式让人很难受。
参考: