# flask-study **Repository Path**: sworker/flask-study ## Basic Information - **Project Name**: flask-study - **Description**: 学习flask内容仓库 - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2023-11-28 - **Last Updated**: 2023-11-29 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # Flask前期准备(环境配置) ## 创建虚拟环境 > windows平台 ```powershell mkdir projectDir # 创建项目目录 cd projectDir # 进入项目目录 py -3 -m venv .venv # 创建虚拟环境 ``` > Linux平台 ```shell mkdir projectDir cd projectDir python3 -m venv .venv ``` ## 激活虚拟环境 > Windows平台 ```powershell .venv\Scripts\activate # 先进入到项目目录再执行 ``` > Linux平台 ```shell . .venv/bin/activate ``` ## 安装Flask ```shell pip install Flask # ubuntu下如果提示pip command not found,执行 apt install python3-pip # centos下,执行 yum install python3-pip ``` # 简单的开始 ## 最简单的代码 ```python from flask import Flask app = Flask(__name__) @app.route('/') def index(): return "Hello World" if __name__=='__main__': app.run(host='0.0.0.0', port=5000, debug=True) ``` # 设置配置文件 ## 查看配置文件 ```python print(app.config) # 配置文件是字典格式,key:value ``` ### 配置文件样式 ```python """ """ ``` ## 设置配置文件 ### 直接赋值 ```python app.config['DEBUG'] = True app.config['TESTING'] = True ... ``` ### 使用配置文件 #### 创建配置文件 setting.py ```python # setting.py DEBUG = True TESTING = True ... ``` #### 使用配置文件 ```python # 导入文件 import settings ... app.config.from_object(settings) ... # 另一种导入方式 app.config.from_pyfile('settings.py') ``` # 路由 ## 基础使用(装饰器) ```python ... @app.route('/') # 这个装饰器就是将rule字符串跟视图函数进行了绑定,通过add_url_rule()实现绑定 def index(): return "Hello World" ... ``` ## 等效写法 ```python ... def index(): return "Hello World" app.add_url_rule('/', view_func=index) ... ``` ## 路由变量规则 ### 使用方法 ```python ... @app.route('/path/') def index(value): do something return "HHHH" ... ``` ### 参数类型 | 类型 | 说明 | 使用 | | ------ | ------------------------------------ | ------------------------------------ | | string | 默认该类型,接收不带斜杠的内容 | @app.route('/path/`'` | | int | 整数 | @app.route('/path/`'` | | float | 浮点型数据(小数) | @app.route('/path/`'` | | path | 路径,类似字符串类型,但是能接收斜杠 | @app.route('/path/`'` | | uuid | 接收UUID类型的字符串类型,比较特殊 | @app.route('/path/`'` | > 补充内容:uuid为唯一标识,python中有uuid库 ```python import uuid # 利用uuid4生成uuid uid = uuid.uuid4() # 结果为 UUID('6eb6624e-fbc3-49be-ba34-15015afca141') # 转换为没有“-”的string类型 strUid = uid.hex # 结果为 '6eb6624efbc349beba3415015afca141' ``` # Response > 返回的对象是一个response > > 可以使用make_response生成response # Request > render_templete:使用的是Jinjia2模板引擎将内容转换为字符串。 ```python from flask import render_templete # 默认路径是 templetes r = render_templete('index.html') ``` ## 传参内容 > request默认接收方法为"GET",需要显示的设置才能修改 ```python @app.route('/', methods=['GET', 'POST') def register(): print(request.full_path) print(request.args) print(request.args.get('username')) # 只有在使用get方法时才能这样获取数据 print(request.form) print(request.form.get('username')) # 使用POST方法时使用该方法,传入的内容放到了体中 ``` > 补充内容:html表单。 1. 里面的method可以修改为post,这样默认的get方法就无法使用。 2. action="/register2"对应的是renderTemplete.py里面的@app.route('/register2') ```html

```