Flask中的路由系统其实我们并不陌生了,从一开始到现在都一直在应用

@app.route("/",methods=["GET","POST"])

为什么要这么用?其中的工作原理我们知道多少?

@app.route() 装饰器中的参数

methods

methods : 当前 url 地址,允许访问的请求方式,默认是GET

@app.route('/login', methods=['GET', 'POST'])
# methods指定这个列表后,只能有这几种请求方式,其他的都或被拒绝
def login():
if request.method == "GET":
return render_template("login.html")
else:
session['user'] = request.form.get('username')
return redirect("/")

endpoint

endpoint : 反向url地址,默认为视图函数名 (url_for)

from flask import url_for
# 如果定义一个验证session的装饰器,需要应用在多个视图函数时候,会报错
@app.route('/', endpoint="this_is_index") # 解决报错的其中之一方法就是 反向地址 endpoint
# 由于两个视图函数都使用同一个装饰时候,相当于两个视图函数重名了,都成了装饰其中的inner函数,这个时候会报错
# 错误信息类似这种AssertionError: View function mapping is overwriting an existing endpoint function: inner
@wrapper def index():
  print(url_for("this_is_index"))
return render_template("index.html")

defaults

defaults : 视图函数的参数默认值{"nid":100}

from flask import url_for

@app.route('/', endpoint="this_is_index", defaults={"nid": })
@wrapper
def index(nid):
print(url_for("this_is_index")) # 打印: /
print(nid) # 浏览器访问下"/", 打印结果:
return render_template("index.html")

strict_slashes

strict_slashes : url地址结尾符"/"的控制 False : 无论结尾 "/" 是否存在均可以访问 , True : 结尾必须不能是 "/"

@app.route('/login', methods=['GET', 'POST'], strict_slashes=True)
# 加上trict_slashes=True之后 在浏览器访问login后边加上/就访问不到了 提示 not fount
def login():
if request.method == "GET":
return render_template("login.html")
else:
session['user'] = request.form.get('username')
return redirect("/")

redirect_to

redirect_to : url地址重定向

@app.route('/detail', endpoint="this_is_detail", redirect_to="/info")  # 此时访问 就会永久重定向到  /info
@wrapper
# AssertionError: View function mapping is overwriting an existing endpoint function: inner
# 由于使用内部函数想当于把视图函数名字都修改为了inner 所以报错
# 解决方法 使用 反向地址 endpoint
def detail():
return render_template("detail.html") @app.route('/info')
def info():
return "hello world"

subdomain

subdomain : 子域名前缀 subdomian="crm" 这样写可以得到 crm.hello.com 前提是app.config["SERVER_NAME"] = "oldboyedu.com"

app.config["SERVER_NAME"] = "hello.com"

@app.route("/info",subdomain="crm")
def student_info():
return "Hello world" # 访问地址为: crm.hello.com/info

动态参数路由:

# 使用方法:/<int:nid>  /<string:str> /<nid>   # 默认字符创

@app.route('/info/<id>')
def info(id):
print(id) # 浏览器url中输入/info/ 打印结果:
return "hello world"

源码以后解析以后再补

第七篇 Flask 中路由系统以及参数的更多相关文章

  1. Flask最强攻略 - 跟DragonFire学Flask - 第七篇 Flask 中路由系统

    Flask中的路由系统其实我们并不陌生了,从一开始到现在都一直在应用 @app.route("/",methods=["GET","POST" ...

  2. 第七篇 Flask 中路由系统

    1. @app.route() 装饰器中的参数 如果不明白装饰器 点击这里 methods : 当前 url 地址,允许访问的请求方式 @app.route("/info", me ...

  3. Flask中路由系统以及蓝图的使用

    一.Flask的路由系统 1.@app.route()装饰器中的参数 methods:当前URL地址,允许访问的请求方式 @app.route("/info", methods=[ ...

  4. 7,Flask 中路由系统

    Flask中的路由系统 @app.route("/",methods=["GET","POST"]) 为什么要这么用?其中的工作原理我们知道 ...

  5. flask中路由系统

    flask中的路由我们并不陌生,从一开始到现在都一直在应用 @app.route("/",methods=["GET","POST"]) 1 ...

  6. Flask中路由系统、Flask的参数及app的配置

    @app.route('/', methods=['GET', 'POST']) 1. @app.route()装饰器中的参数 methods:当前URL地址,允许访问的请求方式 @app.route ...

  7. Flask 中路由系统

    1. @app.route() 装饰器中的参数 methods : 当前 url 地址,允许访问的请求方式 @app.route("/info", methods=["G ...

  8. 第九篇 Flask 中的蓝图(BluePrint)

    第九篇 Flask 中的蓝图(BluePrint)   蓝图,听起来就是一个很宏伟的东西 在Flask中的蓝图 blueprint 也是非常宏伟的 它的作用就是将 功能 与 主服务 分开怎么理解呢? ...

  9. Flask 的路由系统 FBV 与 CBV

    Flask的路由系统 本质: 带参数的装饰器 传递函数后 执行 add_url_rule 方法 将 函数 和 url 封装到一个 Rule对象 将Rule对象 添加到 app.url_map(Map对 ...

随机推荐

  1. spring security oauth2 client_credentials模

    spring security oauth2 client_credentials模 https://www.jianshu.com/p/1c3eea71410e 序 本文主要简单介绍一下spring ...

  2. vue实战记录(四)- vue实现购物车功能之过滤器的使用

    vue实战,一步步实现vue购物车功能的过程记录,课程与素材来自慕课网,自己搭建了express本地服务器来请求数据 作者:狐狸家的鱼 本文链接:vue实战-实现购物车功能(四) GitHub:sue ...

  3. cdq分治(hdu 5618 Jam's problem again[陌上花开]、CQOI 2011 动态逆序对、hdu 4742 Pinball Game、hdu 4456 Crowd、[HEOI2016/TJOI2016]序列、[NOI2007]货币兑换 )

    hdu 5618 Jam's problem again #include <bits/stdc++.h> #define MAXN 100010 using namespace std; ...

  4. Qt调用自己编译的libglog.a出现问题

    我确定依据正确导入库后,依旧出现未定义的引用. undefined reference to _imp___ZN6google17InitGoogleLoggingEPKc 尝试过重新编译,调整编译参 ...

  5. <二>ELK-6.5.3学习笔记–使用rsyslog传输管理nginx日志

    http://www.eryajf.net/2362.html 转载于 本文预计阅读时间 28 分钟 文章目录[隐藏] 1,nginx日志json化. 2,发送端配置. 3,接收端配置. 4,配置lo ...

  6. filebeat+logstash配置

    一. filebeat.yml的配置 filebeat.prospectors:- input_type: log paths: - /tmp/logs/optimus-activity-api.lo ...

  7. Shell脚本之grep

    1. 过滤空行   grep -v ^$

  8. 第十四节: EF的三种模式(四) 之 原生正宗的 CodeFirst模式的默认约定

    一. 简介 1. 正宗的CodeFirst模式是不含有edmx模型,需要手动创建实体.创建EF上下文,然后生成通过代码来自动映射生成数据库. 2. 旨在:忘记SQL.忘记数据库. 3. 三类配置:On ...

  9. IISExpress使用64位

    C#有一些函数如GetHashCode和x86,X64版本有关系,为了和服务器保持一致,本地iis Express也需要设置64位. 方法如下,vs2010不支持. vs2012可以手动添加配置 re ...

  10. nnet3 TDNN chunk, left-context, right-context

    chunk-width 数据块的宽度 NnetIo name=="input" indexes,left-context+num-frame+right-context=5+7+6 ...