3、url_for反向解析

作用:url_for获取蓝图里面函数的动态路径

用法:

 res = url_for("蓝图的名字.函数的名字"[,参数1=值1参数2=值2...])

注意:该url_for会得到你调用函数的路径,后面跟不跟参数根据你调用的函数是不是有参数,如果没有你传入参数之后变成值的形式。比如:http://106.12.109.69:8000/path/2?a=3;变成?变量=值的形式

后端的写法

redirect(res):重定向url_for得到的函数的路径

举例:

第一种不需要参数的函数:

@blue.route("/index/")

def index():

 return "我是index"

@blue.route("/check/")

def check_url():

res = url_for("first.index")

print(res)

  return redirect(res)

第二种需要参数的函数

@blue.route("/path/")

def path(id):

    return "你的参数是%d" %id

@blue.route("/check/")

def check_url():

    res = url_for("first.path",id=2)

    print(res)

    return redirect(res)

有代码分析:

链接: https://pan.baidu.com/s/13kvs6zMm00_XeDY_s8QAJQ 提取码: ks3i

你可能感兴趣的:(3、url_for反向解析)