第七篇 Flask 中路由系统以及参数
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 中路由系统以及参数的更多相关文章
- Flask最强攻略 - 跟DragonFire学Flask - 第七篇 Flask 中路由系统
Flask中的路由系统其实我们并不陌生了,从一开始到现在都一直在应用 @app.route("/",methods=["GET","POST" ...
- 第七篇 Flask 中路由系统
1. @app.route() 装饰器中的参数 如果不明白装饰器 点击这里 methods : 当前 url 地址,允许访问的请求方式 @app.route("/info", me ...
- Flask中路由系统以及蓝图的使用
一.Flask的路由系统 1.@app.route()装饰器中的参数 methods:当前URL地址,允许访问的请求方式 @app.route("/info", methods=[ ...
- 7,Flask 中路由系统
Flask中的路由系统 @app.route("/",methods=["GET","POST"]) 为什么要这么用?其中的工作原理我们知道 ...
- flask中路由系统
flask中的路由我们并不陌生,从一开始到现在都一直在应用 @app.route("/",methods=["GET","POST"]) 1 ...
- Flask中路由系统、Flask的参数及app的配置
@app.route('/', methods=['GET', 'POST']) 1. @app.route()装饰器中的参数 methods:当前URL地址,允许访问的请求方式 @app.route ...
- Flask 中路由系统
1. @app.route() 装饰器中的参数 methods : 当前 url 地址,允许访问的请求方式 @app.route("/info", methods=["G ...
- 第九篇 Flask 中的蓝图(BluePrint)
第九篇 Flask 中的蓝图(BluePrint) 蓝图,听起来就是一个很宏伟的东西 在Flask中的蓝图 blueprint 也是非常宏伟的 它的作用就是将 功能 与 主服务 分开怎么理解呢? ...
- Flask 的路由系统 FBV 与 CBV
Flask的路由系统 本质: 带参数的装饰器 传递函数后 执行 add_url_rule 方法 将 函数 和 url 封装到一个 Rule对象 将Rule对象 添加到 app.url_map(Map对 ...
随机推荐
- elastalert 配置post告警方式(备忘)
最近在做把elk告警日志发送到kinesis 流,供后续数据分析处理使用........ 基于尽量不修改elastalert ,把修改工作放到接收端服务的原则.计划把elk的告警数据通过远程api ...
- 测试框架httpclent 3.获取cookie的信息,然后带cookies去发送请求
在properties文件里面: startupWithCookies.json [ { "description":"这是一个会返回cookies信息的get请求&qu ...
- 礼物(中国剩余定理+拓展gcd求逆元+分治=拓展Lucus)
礼物 题意: 求\[C(n,m)\ \%\ p\] \(n,m,p\le 10^9\),且若\(p=\prod_{i=1}^{k}{p_i}^{c_i}\),则\(\forall i\in [1..k ...
- 使用WebClient进行文件上传
注释部分为异步上传,几行代码就能搞定 public static bool Upload(string url, string path) { using (WebClient client = ne ...
- -bash: Chmod: command not found
是增加该文件的所有者拥有运行权限 如果所有者是root ,还要加sudo chmod u+x drlinuxclient.bin (sudo) chmod u+x drlinuxclient.bin ...
- Arduino传感器学习目录
Arduino-接口图 在Windows上安装Arduino-IDE 函数库和程序架构介绍 Arduino语法-变量和常量 Arduino常用的数据类型以及转换 Arduino—运算符 ...
- python中字符串编码转换
字符串编码转换程序员最苦逼的地方,什么乱码之类的几乎都是由汉字引起的. 其实编码问题很好搞定,只要记住一点: 任何平台的任何编码,都能和Unicode互相转换. UTF-8与GBK互相转换,那就先把U ...
- [Linux容器]当我们谈容器的时候,我们在谈什么
Docker在当下很火,那么,当我们谈Docker,谈容器的时候,我们在谈什么? 或者说,你对Docker,对容器了解吗?容器,到底是怎么一回事儿? 这篇文章着重来讲一下Linux容器,为什么强调Li ...
- tengine2.2.3报错502的The proxy server received an invalid response from an upstream server问题处理
tengine2.2.3报错502的The proxy server received an invalid response from an upstream server问题处理 现象:访问订单的 ...
- EHCache:Eelment刷新后,timeToLiveSeconds失效了?
个人以为只要设定了timeToLiveSeconds,中间过程不管有没有访问,只要LiveSeconds时间到了,缓存就会失效.但是开发时发现并非如此,经过一番折腾,最终发现自己的理解是正确的,还是使 ...