第七篇 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对 ...
随机推荐
- CentOS7使用firewalld防火墙配置端口
安装启用firewalld防火墙 CentOS7默认的防火墙是firewalld 如果没有firewalld防火墙,可以执行yum install firewalld 命令进行安装 firewalld ...
- 越光后端开发——ygapi(1.新建项目ygapi、新建MySQL数据库yg、项目连接数据库)
1.新建MySQL数据库 show databases;//查看已经有的数据库 create database yg; 2.新建项目ygapi 1.使用pycharm新建django项目取名ygapi ...
- Springboot 3.需求携带参数的get请求
还是拿来上节讲的代码: package com.course.server; import org.springframework.web.bind.annotation.*; import java ...
- (二叉树 DFS 递归) leetcode 112. Path Sum
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...
- SpringBoot使用消息队列RabbitMQ
RabbitMQ 即一个消息队列,主要是用来实现应用程序的异步和解耦,同时也能起到消息缓冲.消息分发的作用.RabbitMQ是实现AMQP(高级消息队列协议)的消息中间件的一种,AMQP,即Advan ...
- Self-organizing Maps及其改进算法Neural gas聚类在异常进程事件识别可行性初探
catalogue . SOM简介 . SOM模型在应用中的设计细节 . SOM功能分析 . Self-Organizing Maps with TensorFlow . SOM在异常进程事件中自动分 ...
- .net core 多租户框架整理
一 saaskitAsp.Net Core multi-tenant application Sample using #SaaSKithttps://github.com/saaskit/saask ...
- Mysql MGR架构误操作引发的问题处理
[背景介绍] 故障方描述:一次用户刷权限的时候不小心把数据库用户表记录删掉了,执行之后发现不对后重建用户,杀掉进程后重新MGR启动报错. [报错信息] 2018-06-13T12:47:41.4055 ...
- [物理学与PDEs]第5章习题4 广义 Hookean 定律的张量的对称性
设材料是超弹性的, 并设参考构形为自然状态, 证明由线性化得到的张量 ${\bf A}=(a_{ijkl})=\sex{2\cfrac{\p \bar p_{ij}}{c_{kl}}}$ 具有以下的对 ...
- [物理学与PDEs]第3章习题2 仅受重力作用的定常不可压流理想流体沿流线的一个守恒量
设定常 (即 $\cfrac{\p {\bf u}}{\p t}={\bf 0}$).不可压缩 (设 $\rho=1$) 的理想流体所受的体积力仅为重力. 又设磁场满足条件: $({\bf H}\cd ...