8, Flask Combat 8th day: Add_url_rule and App.route principle

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.