A two set of routing methods
1, decorator
@app.route('/index/') def index(): return 'Hello World!'
2, source
route->decorator->add_url_rule(rule, endpoint, f, **options)
def index(): return 'Hello World!' app.add_url_rule('/index/', None, index)
Note: The value of the endpoint can not be the same
Second, the parameters
rule URL rules view_func view function names Endpoint = None name, the URL for generating reverse, namely: the url_for ( ' name ' ) Methods = None mode allows a request, such as: [ " the GET " , " the POST " ] strict_slashes = None of the final URL / whether the symbol stringent requirements, the redirect_to = None redirected to the specified address defaults = None default values, when no URL parameters, function requires parameters, = {defaults ' K ' : ' V ' } as a function of parameters provided subdomain = None, sub-domain access
endpoint, metods, in front of introduction
1, redirect_to redirection
When the access rule, redirect to the destination URL
Role: a Web site upgrade and maintenance, via this method the user need not know the address of the new site, you can jump to the specified URL from the old URLs
from flask import Flask app = Flask(__name__) @app.route('/index/', redirect_to='https://www.bootcss.com/') def index(): return 'Hello World!' # app.add_url_rule('/index/', None, index) if __name__ == '__main__': app.run()
2、strict_slashes
= the Flask strict_slashes # lax url last slash requirements
3, subdomain (understand)
from flask import Flask app = Flask(import_name=__name__) app.config['SERVER_NAME'] = 'api.com:5000' """ 127.0.0.1 api.com 127.0.0.1 web.api.com 127.0.0.1 admin.api.com """ # http://admin.api.com:5000/admin @app.route("/admin", subdomain="admin") def admin_index(): return "admin.your-domain.tld" # http://web.api.com:5000/ @app.route("/", subdomain="web") def web_index(): return "web.your-domain.tld" # http://sdsdf.api.com:5000/ # http://sdfsdf.api.com:5000/ # http://asdf.api.com:5000/ @app.route("/dynamic", subdomain="<username>") def username_index(username): """Dynamic subdomains are also supported Try going to user1.your-domain.tld/dynamic""" return username + ".your-domain.tld" if __name__ == '__main__': app.run()
Three, CBV (almost no)
from functools import wraps from flask import Flask, views app = Flask(__name__) # 4 def wrapper(func): @wraps(func) def inner(*args, **kwargs): return func(*args, **kwargs) return inner # 1 class UserView(views.MethodView): # 3 methods = ['GET'] decorators = [wrapper] def get(self, *args, **kwargs): return 'GET' def post(self, *args, **kwargs): return 'POST' # 2 app.add_url_rule('/user/', None, UserView.as_view('endpoint')) if __name__ == '__main__': app.run()
Fourth, the custom regular
from the Flask Import the Flask, url_for from werkzeug.routing Import BaseConverter App = the Flask ( __name__ ) # Step one: custom class class RegexConverter (BaseConverter): "" " Custom URL match the regular expression " "" DEF __init__ (Self, the Map, REGEX): Super (RegexConverter, Self). the __init__ (Map) self.regex = REGEX DEF to_python (Self, value): "" " routing match, a match is passed to the parameter value in view of the function : param value: : return: "" " return int (value) DEF to_url (Self, value): "" " reverse URL is generated using the url_for, parameters passed through the process processing and returns the URL value of the parameter used to generate : param value: : return: " "" Val = Super (RegexConverter, Self) .to_url (value) return Val # Second step: to converter app.url_map.converters [ ' REG ' ] = RegexConverter "" " 1. a user sends a request internal 2. flask for match regular 3. call to_python (regular match result) method 4. to_python method return value to the view function of "" " # step 3: use the custom regular @ app.route ( '/index/<reg("\d+"):nid>/') def index(nid): print(nid, type(nid)) # 4 <class 'int'> print(url_for('index', nid=110)) # /index/110/ return "index" if __name__ == '__main__': app.run()