3. Flask routing system

A commonly used routing system

- @app.route('/user/<username>')
- @app.route('/post/<int:post_id>')
- @app.route('/post/<float:post_id>')
- @app.route('/post/<path:path>')
- @app.route('/login', methods=['GET', 'POST'])

Second, routing parameters

1. methods

methods: the current url address to allow access request, the default does not write for the GET method

@app.route("/info", methods=["GET", "POST"])
def student_info():
    stu_id = int(request.args["id"])
    # Python3.6的新特性 f"{变量名}"
    return f"Hello Old boy {stu_id}"  

2. endpoint

endpoint: Reverse url address, default career function name (url_for)

from flask import url_for

@app.route("/info", methods=["GET", "POST"], endpoint="r_info")
def student_info():
    print(url_for("r_info"))  # /info
    stu_id = int(request.args["id"])
    return f"Hello Old boy {stu_id}" 

3. defaults

defaults: Parameter Default view function { "nid": 1}

from flask import url_for

@app.route("/info", methods=["GET", "POST"], endpoint="r_info", defaults={"nid": 100})
def student_info(nid):  #这里形参必须有个nid接收
    print(url_for("r_info"))  # /info
    # stu_id = int(request.args["id"])
    print(nid)  # 100
    return f"Hello Old boy {nid}"

4. strict_slashes

strict_slashes: url address ending character "/" control; False: Whether the end of "/" if there is accessible; True: the end must not be "/"

# 访问地址 : /info 
@app.route("/info", strict_slashes=True)
def student_info():
    return "Hello Old boy info"
# 访问地址 : /infos  or  /infos/
@app.route("/infos", strict_slashes=False)
def student_infos():
    return "Hello Old boy infos"

5. redirect_to

redirect_to: url address redirection, and 308 permanent redirect

# 访问地址 : /info 浏览器跳转至 /infos
@app.route("/info", strict_slashes=True, redirect_to="/infos")
def student_info():
    return "Hello Old boy info"
@app.route("/infos", strict_slashes=False)
def student_infos():
    return "Hello Old boy infos"

6. subdomain

subdomain: subdomain prefix subdomian = "cnblogs" wrote cnblogs.aspx.com can be provided that the app.config [ "SERVER_NAME"] = "aspx.com"

app.config["SERVER_NAME"] = "aspx.com"
@app.route("/info",subdomain="cnblogs")
def student_info():
    return "Hello cnblogs info"
# 访问地址为:  cnblogs.aspx.com/info

7. Dynamic routing parameters

It is the definition of a parameter after receiving the url; but this dynamic routing parameters, in url_for time, be sure to dynamic parameters name + parameter value added to it, otherwise it will throw an exception parameter error

from flask import url_for

# 访问地址 : http://127.0.0.1:5000/info/1
@app.route("/info/<int:nid>", methods=["GET", "POST"], endpoint="r_info")
def student_info(nid):
    print(url_for("r_info",nid=2))  # /info/2
    return f"Hello Old boy {nid}" 
1. 多个参数传递

from flask import url_for
# 访问地址 : http://127.0.0.1:5000/info/1
@app.route("/info/<nid1>/<nid2>", methods=["GET", "POST"], endpoint="r_info")
def student_info(nid):
    print(url_for("r_info",nid=2,nid=3))  # /info/2/3
    return f"Hello Old boy {nid}" 

Guess you like

Origin www.cnblogs.com/hq82/p/12636187.html