概述
请求地址会被底层Werkzeug库处理(去除ip和端口),用于进行路由匹配,匹配成功后将找打的视图函数进行调用,处理本次用户的请求
格式
url_map属性
功能:查看当前所有的路由配置
使用:app.url_map
无参路由
<span class="hljs-meta">@app.route('/index/')</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">index</span><span class="hljs-params">()</span>:</span>
<span class="hljs-keyword">return</span> <span class="hljs-string">'Hello Flask'</span>
传参路由
A、带一个参数的路由
<span class="hljs-comment"># 带参路由,参数写在<>中</span>
<span class="hljs-meta">@app.route('/arg/<name>')</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">arg</span><span class="hljs-params">(name)</span>:</span>
<span class="hljs-keyword">return</span> <span class="hljs-string">'Hello %s !'</span> % name
B、带多个参数的路由
<span class="hljs-meta">@app.route('/args/<name>_<age>')</span>
<span class="hljs-meta">@app.route('/args/<name>/<age>')</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">args</span><span class="hljs-params">(name, age)</span>:</span>
print(name, age)
<span class="hljs-keyword">return</span> name, age
C、参数类型限制
<span class="hljs-meta">@app.route('/welcome/<string:name>/<int:age>/<float:height>/<path:pstr>/')</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">welcome</span><span class="hljs-params">(name, age, height, pstr)</span>:</span>
print(type(name),type(age), type(height), type(pstr))
print(name, age, height, pstr)
<span class="hljs-keyword">return</span> <span class="hljs-string">'类型限制'</span>
注意:
D、正则约束
作用
使用正则表达式对路由进行更具体的约束
正则转换类(app.py)
<span class="hljs-keyword">from</span> werkzeug.routing <span class="hljs-keyword">import</span> BaseConverter
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">RegexConverter</span><span class="hljs-params">(BaseConverter)</span>:</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span><span class="hljs-params">(self, url, *args)</span>:</span>
super().__init__(url)
self.regex = args[<span class="hljs-number">0</span>]
给app对象注册路由转换类
app.url_map.converters[<span class="hljs-string">"regex"</span>] = RegexConverter
使用
<span class="hljs-meta">@app.route("/test_regex/<regex('\d{2}'):age>/")</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">test_regex</span><span class="hljs-params">(age)</span>:</span>
<span class="hljs-comment"># 正则约束是约束的值的样子,不会约束类型</span>
print(age)
<span class="hljs-keyword">return</span> age
HTTP方法
概述:默认视图可以被GET访问,flask中可以规定以什么样的方式请求视图,如果使用不正确的请求方式会返回405错误
GET
<span class="hljs-meta">@app.route("/get/")</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">test_get</span><span class="hljs-params">()</span>:</span>
<span class="hljs-keyword">return</span> <span class="hljs-string">"GET"</span>
POST
<span class="hljs-meta">@myApp.route("/post/", methods=["POST"])</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">test_post</span><span class="hljs-params">()</span>:</span>
<span class="hljs-keyword">return</span> <span class="hljs-string">"POST"</span>
GET OR POST
<span class="hljs-meta">@myApp.route("/get_post/", methods=["GET", "POST"])</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">test_get_post</span><span class="hljs-params">()</span>:</span>
<span class="hljs-keyword">return</span> <span class="hljs-string">"test_get_post"</span>
http请求方式