第七篇 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对 ...
随机推荐
- [NOI2018]屠龙勇士
题目描述 题解 考虑增量法. 假设我们已经做完了前k个条件,前面的模数连乘起来的结果为M,答案为X,当前的攻击力为x,龙的血量为a. 那么我们这一次的答案的表达形式是X+t*M的. 这一次需要满足的是 ...
- TensorFlow迁移学习的识别花试验
最近学习了TensorFlow,发现一个模型叫vgg16,然后搭建环境跑了一下,觉得十分神奇,而且准确率十分的高.又上了一节选修课,关于人工智能,老师让做一个关于人工智能的试验,于是觉得vgg16很不 ...
- Pandas系列(十)-转换连接详解
目录 1. 拼接 1.1 append 1.2 concat 2. 关联 2.1 merge 2.2 join 数据准备 # 导入相关库 import numpy as np import panda ...
- EF CodeFirst系列(1)---CodeFirst简单入门
1.什么是CodeFirst 从EF4.1开始,EF可以支持CodeFirst开发模式,这种开发模式特别适用于领域驱动设计(Domain Driven Design,大名鼎鼎的DDD).在CodeFi ...
- mui选择器的坑
mui框架最近比较火,因为在移动端的页面展示效果太好了,web页面相当于APP的效果.连二年级的小明同学都知道了..你别说你不知道哦 但是这毕竟是一个不成熟的框架,维护和解决方案都跟不上,因此新手入坑 ...
- DirectX11 With Windows SDK--12 深度/模板状态、平面镜反射绘制
前言 深度/模板测试使用的是与后备缓冲区同等分辨率大小的缓冲区,每个元素的一部分连续位用于深度测试,其余的则用作模板测试.两个测试的目的都是为了能够根据深度/模板状态需求的设置来选择需要绘制的像素. ...
- CSS font字体知识学习
字体系列 [1]5种通用字体系列:拥有相似外观的字体系列 serif字体:字体成比例,且有上下短线(衬线字体),包括Times\Georgia\New century Schoolbook sans- ...
- [再寄小读者之数学篇](2014-10-27 Frobenius 范数是酉不变范数)
对任两酉阵 $U,V$, 有 $$\bex \sen{A}_F=\sen{UAV}_F. \eex$$ 事实上, $$\beex \bea \sen{UAV}_F^2&=\tr(V^*A^*U ...
- tomcat7的安装与maven安装
tomcat7的安装与配置: 下载tomcat7 :wget 地址 解压:tar -zxvf 文件名 编辑tomcat目录下的conf下的server.xml文件: <Connector por ...
- CMake set 语法
参考CMake官方文档:https://cmake.org/cmake/help/v3.14/command/set.html 1. 普通变量 set(<variable> <val ...