October 2018
Intermediate to advanced
332 pages
8h 9m
English
The url_for function returns the URL of a route by giving the route function name as a parameter, as shown in the following code. This allows URLs to be changed without worrying about where links will break:
{{ url_for('home') }}
/
Here, home is the name of a function that is registered as an endpoint on Flask, and the relative URL root associated with it, so on our main.py, we must define a function to deal with the HTTP request and register it on Flask using the decorator app.route(rule, **options), as shown in the following code:
@app.route('/')def home():...
If we had a route that had positional arguments in the URL, we pass them as kwargs. They will be filled in for us in the resultant URL as follows:
{{ url_for('post', ...