Settings page and request routing methods: Flask

1, Flask routing implementation

  Flask route by route decorators to achieve

@app.route("/index/")
def index():
    return "hello flask"

note:

(1) Flask route must be "/" beginning, otherwise it will error:

if not string.startswith("/"):
    raise ValueError("urls must start with a leading slash")

  The above is a Flask regarding the route to the source code can be seen that, if the route is not in "/" at the beginning, will be thrown "urls must start with a leading slash " Exception

(2) In general, the routing function of the same name should appear fairly standard view, the same can not write the name of the view function

2, Flask pass routing parameters

  In the Flask in mass participation without the use of regular expressions:

@app.route("/index/<name>/<int:age>/")
def index(name, age):
    return "hello {},I'm {} years old".format(name, age)

operation result:

  Common parameter types are: str string (the default), int int, float float, path path

note:

  Parameter location view can freely exchange function, but the routing address to be accessed must follow the route passaged decorator reference position in the route parameters

3, Flask request is set

  In Flask , only you need route add a decorative and set methods parameters to complete the setting of the view mode request

@app.route("/index/<name>/<int:age>/", methods=["GET", "POST"])
def index(name, age):
    return "hello {},I'm {} years old".format(name, age)

Check the following source code:

if methods is None:
    methods = getattr(view_func, "methods", None) or ("GET",)

  As can be seen, Methods value default ( "GET",) , i.e., default view mode as function request "GET" request

Guess you like

Origin www.cnblogs.com/xmcwm/p/11794459.html
Recommended