第七篇 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对 ...
随机推荐
- 解决Parameter '__frch_item_0' not found. Available parameters 问题
1. 问题描述: 出现如下问题,执行报错信息 Caused by: org.mybatis.spring.MyBatisSystemException: nested exception is org ...
- (五)qt资源文件
// 规则: :+添加的前缀/+文件名 ui->actionSave_as->setIcon(QIcon(":/new/Image/face.png"));
- Slow ReadProcessor&Error Slow BlockReceiver错误日志分析(转)
1.总结 "Slow ReadProcessor" 和"Slow BlockReceiver"往往是因为集群负载比较高或者某些节点不健康导致的,本文主要是帮助你 ...
- 第六节: 六类Calander处理六种不同的时间场景
背景介绍及其使用 该章节主要补充介绍,在前一章四类触发器的基础上配合六大Canlander来动态删减某些时间,来满足更多的应用场景. 1. DailyCalendar:动态排除某天的某些字段. (需求 ...
- 第二十三节: EF性能篇(三)之基于开源组件 Z.EntityFrameWork.Plus.EF6解决EF性能问题
一. 开篇说明 EF的性能问题一直以来经常被人所吐槽,究其原因在于“复杂的操作在生成SQL阶段耗时长,且执行效率不高”,但并不是没有办法解决,从EF本身举几个简单的优化例子: ①:如果仅是查询数据,并 ...
- django - 总结 - form表单
{{ form.as_table }} 以表格的形式将它们渲染在<tr> 标签中 {{ form.as_p }} 将它们渲染在<p> 标签中 {{ form.as_ul }} ...
- $L^p$ 调和函数恒为零
设 $u$ 是 $\bbR^n$ 上的调和函数, 且 $$\bex \sen{u}_{L^p}=\sex{\int_{\bbR^n}|u(y)|^p\rd y}^{1/p}<\infty. \e ...
- SQL Server 跨服务器操作
Ø 简介 在工作中编写 SQL 时经常会遇到跨库或跨服务器操作,比如查询时,通过 A 服务器的某张表关联 B 服务器某张表,进行连接查询.或者从另一台服务器中的数据,对当前数据库中的数据进行 CRU ...
- Java(6)for循环
一.for循环的使用场合 1.1.while循环——先判断,再循环 while(1.条件表达式){ //2.循环操作 //3.更改循环条件表达式 } 1.2.do…while——先 ...
- Codeforces Round #527 (Div. 3) . F Tree with Maximum Cost
题目链接 题意:给你一棵树,让你找一个顶点iii,使得这个点的∑dis(i,j)∗a[j]\sum dis(i,j)*a[j]∑dis(i,j)∗a[j]最大.dis(i,j)dis(i,j)dis( ...