Before we used the @app.route to bind the view function and the URL
@app. Route ('/')def Hell_world (): return' Helloworld'
And we can get the URL '/' by url_for (' Hello_world ')
In fact, we can add the endpoint parameter to this decorator and name the URL.
@app. Route ('/', endpoint='index')def Hello_word (): return ' Hello World '
Once we have used the endpoint parameter, we cannot use the name of the view function when we use url_for () inversion, but we use the URL name we define.
Url_for ('index')
Another way to bind a view function and URL is add_url_rule
Enter App.add_url_rule under app = Flask (__name__), then hold down CTRL and click it to view the source code
rule: Set URL
endpoint: Name set for URL
view_func: View functions
Therefore, we can use this
def my_list (): return ' my list ' app.add_url_rule (Rule='/list/', endpoint='list ', view_func=my_list)
If you want to use url_for inversion: url_for (' list ')
In fact, we look at @app.route the source of this decorator, but also with Add_url_rule
8, Flask Combat 8th day: Add_url_rule and App.route principle