[Flask Routing System

methods

The method of the current view request supported

from flask import Flask,render_template
app = Flask(__name__)

@app.route("/login", methods=["GET", "POST"])
def student_info():
    return "Hello wWrd "

The default parameters do not write methods only supports GET requests, methods parameter is the way we rewrite the inside, not added

endpoint

Route map - a view function

Copy the code
Import the Flask Flask from, the render_template, the redirect 
App = the Flask (name__ __) 

@ app.route ( "/ Login", Methods = [ "the GET", "the POST"], Endpoint = "log") 
DEF student_info (): 
    return "the Hello wWrd " 

# is equivalent to / login name takes the individual, where after the view function is used in the direct path can be resolved to an alias / login path this 

example: 
@ app.route ( '/ index', Methods = [ ' the GET ',' the POST ']) 
DEF index (): 
    return the redirect ( "log")
Copy the code

defaults

Once the default parameters have default parameters must exist to receive a parameter variable name parameter must be consistent with the view function defaults

Import the Flask Flask from, the render_template 
@ app.route ( '/ index', Methods = [ 'the GET', 'the POST'], Defaults = { 'ID': 2}) 
DEF index (ID): This parameter must be followed by # consistent with the defaults key 
    Print (ID) # 2 
    return the render_template ( 'the login.html')

strict_slashes

Whether or not strictly follow the routes matching rules "/"

Copy the code
from flask import Flask,render_template,redirect
app = Flask(__name__)

@app.route("/login", methods=["GET", "POST"],strict_slashes=False)
def student_info():
    return "Hello wWrd "

# 默认遵守严格模式strict_slashes=True
Copy the code

When we request the path on the browser, if the input is below a path then the browser will automatically we add a path separator "/"

strict_slashes = False no matter behind "/" can be matched to

redirect_to

Permanent redirect, and not through the view function

@app.route('/index',methods=['GET', 'POST'],redirect_to='/login')
def index():
    return render_template('index.html')

Dynamic routing parameters

Add additional parameters in the request path

 

Copy the code
Import the Flask Flask from, the render_template, the redirect 
App = the Flask (name__ __) 

@ app.route ( "/ Login / <args> / <int: NUM>", Methods = [ "the GET", "the POST"],) denotes # int must be an integer 
def student_info (args, num): # parameter name must be consistent and routing parameters 
    Print (args, NUM) # 2 aaaa 
    return "the Hello Word"
Dynamic parameters <> package 
<> can be written as the <int: num> so. Int type argument must be given, or will be error
<> within not write, the default is str
Copy the code

Guess you like

Origin www.cnblogs.com/youxiu123/p/11605767.html