Python: How are dynamic Flask routes implemented?

0

I started to study a bit about Web development with Flask. The framework treats dynamic URL components as follows:

@app.route('/<comp_dinamico>')
def page(comp_dinamico):
    # codigo
    return '%s' % comp_dinamico

What I wanted to understand is how the substring in '/<comp_dinamico>' becomes a variable. Or if this is not a variable, I'd like to understand what it is. Thanks in advance.

    
asked by anonymous 13.02.2018 / 13:02

1 answer

0

Flask decomposes its URL into parts for functions that match it. For example, let's say you have a URL to view data for a user that is /user/1 .

Declaring a decorator that matches this URL would look like this:

@app.route('/user/<user_id>')
def my_user(user_id):
    pass

Knowing that this decorator is a match for your route, it injects the part of the URL with the dynamic value into its parameters.

This pattern is found in various frameworks like Django, Laravel, etc.

It is usually the use of regular expressions that provide this functionality.

    
13.02.2018 / 13:54