First, the view function routing rules
from flask Import the Flask, redirect, url_for # create application objects flask # __name__ behalf of the current module name # flask to the current directory List, static directory is a static directory, templates directory as a template App = the Flask ( __name__ ) @ app.route ( " / " ) DEF index (): " "" Custom view function "" " return " Hello World " # by way of access methods defined @ app.route ( " / POST " , methods = [ " the POST " ]) DEF POST () : return "do post" @ App.route ( " / Hi1 " ) @ app.route ( " / HI2 " ) DEF hi (): return " hi Page " @ app.route ( " / the Login " ) DEF the Login (): # Use url_for function find url path through the name of the view function url = url_for ( " index " ) return redirect (url) IF __name__ == ' __main__ ' : # see all routes Print (App. url_map) # start the application app.run(debug=True)
Second, the routing parameter extraction with a custom converter
from flask Import the Flask, redirect, url_for from werkzeug.routing Import BaseConverter # Create a flask application object # __name__ behalf of the current module name # flask to the current directory List, static directory is a static directory, templates directory as a template App = the Flask ( __name__ ) # routing switches int, a float, path # http://127.0.0.1:5000/goods/12 @ app.route ( " / Goods / <int: goods_id> " ) # @ app.route ( "/ Goods / < goods_id> ") may be DEF goods_detail (goods_id): return " goods_detail_% S " % STR (goods_id) # 1, a custom converter class RegexConvert (BaseConverter): DEF the __init__ (Self, url_map, REGEX): # call the parent class initialization method # Super (RegexConvert, Self) .__ the init __ (url_map) BaseConverter. The __init__ (Self, url_map) # the regular expressions stored in the object attribute, the flask will be used to route the regular matching property self.regex = REGEX # 2, from the conversion definition added to the flask app.url_map.converters [ " Re " ] = RegexConvert # 3, using @ app.route ( " / Send / <Re (R'1 [34578] \ {D}. 9 '): Mobile> " ) #http://127.0.0.1:5000/send/18612345678 DEF send_sms (Mobile): return " Send SMS to% S " % Mobile @ app.route ( " / " ) DEF index (): URL = the url_for ( " send_sms " , Mobile = " 18,612,345,678 " ) return redirect (url) IF __name__ == ' __main__ ' : # see all routes Print (app.url_map) # start the application app.run (debug = True)