一、Flask安装环境配置
当前我的开发环境是Miniconda3+PyCharm。开发环境其实无所谓,自己使用python3+Nodepad都可以。安装Flask库:
pip install Flask
二、第一个Flask应用程序
将以下内容保存为helloworld.py:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 导入Flask类
from flask import Flask
# 实例化,可视为固定格式
app = Flask(__name__)
# route()方法用于设定路由;类似spring路由配置
@app.route('/helloworld')
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(host="0.0.0.0", port=5000)
直接运行该文件,然后访问:http://127.0.0.1:5000/helloworld。结果如下图:
三、get和post实现
3.1 创建用到的模板文件
Flask默认到templates目录下查找模板文件,在上边helloworld.py同级目录下创建templates文件夹。
在templates文件夹下创建get.html,写入以下内容:
1
2
3
4
5
6
7
8
9
10
11
12
13
<!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>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。