Flask resolve the endpoint

1. Flask route analytic function

@app.route('/user/<name>')
def user(name):
    return 'Hello, %s' % name

Equivalent to

def user(name)
    return 'Hello, %s' % name
    
app.add_url_rule('/user/<name>', 'user', user)

add_url_ruleFunction in the document explained:

add_url_rule(*args, **kwargs)
Connects a URL rule. Works exactly like the route() decorator. If a view_func is provided it will be registered with the endpoint.

add_url_ruleparameter:

rule – the URL rule as string(匹配的路由地址)
endpoint – the endpoint for the registered URL rule. Flask itself assumes the name of the view function as endpoint
view_func – the function to call when serving a request to the provided endpoint(视图函数)
options – the options to be forwarded to the underlying Rule object. A change to Werkzeug is handling of method options. methods is a list of methods this rule should be limited to (GET, POST etc.). By default a rule just listens for GET (and implicitly HEAD). Starting with Flask 0.6, OPTIONS is implicitly added and handled by the standard request handling.

2. Flask request resolution

Suppose the user access http://www.example.com/user/eric, Flask will find the user function and pass name='eric', execute this function and return values.
In practice, however, Flask really is a direct function according to the Route view it?

In the source code can be found:

  • Each application apphas one view_functions, which is a dictionary , storage endpoint-view_func key-value pairs . The first effect is to add the key to the add_url_rule view_functions of the (run before the application process)
  • Each application apphas a url_map, which is a class Map ( werkzeug/routing.pyin), which contains a list , the list element is Role instance ( werkzeug/routing.pyin) . The second function is to add add_url_rule to url_map Role in Example (treatment prior to the application run)

We can look at an example:

app = Flask(__name__)

@app.route('/test', endpoint='Test')
def test():
    pass


@app.route('/', endpoint='index')
def hello_world():
    return 'Hello World!'

if __name__ == '__main__':
    print(app.view_functions)
    print(app.url_map)
    app.run()

Run the program results:

{'static': <bound method Flask.send_static_file of <Flask 'flask-code'>>, 'Test': <function test at 0x10065e488>, 'index': <function hello_world at 0x10323d488>}

Map([<Rule '/test' (HEAD, OPTIONS, GET) -> Test>,
 <Rule '/' (HEAD, OPTIONS, GET) -> index>,
 <Rule '/static/<filename>' (HEAD, OPTIONS, GET) -> static>])
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)

url_map is stored url and endpoint mapping!
In fact, when the request came a urltime, will first pass ruleto find endpoint(url_map), and then according endpointto find corresponding view_func(view_functions). Usually, endpoint and view names are the same as function names.

实际上这个endpoint就是一个Identifier,每个视图函数都有一个endpoint,
当有请求来到的时候,用它来知道到底使用哪一个视图函数

3. Flask view redirected

In practice, when the need to jump to another view in one view, it often uses url_for(endpoint)to query view, rather than hard-coded addresses to function.
This time, we can not use the name of the view function when the endpoint to query the

app = Flask(__name__)
app.register_blueprint(user, url_prefix='user')
app.register_blueprint(book, url_prefix='book')

More than two registered blueprint

In the user (not the initialization process):

# user.py

@user.route('/article')
def article():
    pass

In the book (not initialization process):

# book.py

@book.route('/article')
def article():
    pass

/articleThis routing function name corresponding to the two views of the same functions, but in two separate blueprint. When url_for(article)calling the time (note, url_forby endpointthe query urladdress, and then find the view function ), Flask not know the endpoint at which the blueprint in the end use, it needs to be defined as follows:

url_for('user.article')

4. Reference

Guess you like

Origin www.cnblogs.com/yueyun00/p/12399223.html