Skip to Content
Mastering Flask Web Development - Second Edition
book

Mastering Flask Web Development - Second Edition

by Daniel Gaspar, Jack Stouffer
October 2018
Intermediate to advanced
332 pages
8h 9m
English
Packt Publishing
Content preview from Mastering Flask Web Development - Second Edition

The url_for() function

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', ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Flask Web Development, 2nd Edition

Flask Web Development, 2nd Edition

Miguel Grinberg
Flask Web Development

Flask Web Development

Miguel Grinberg

Publisher Resources

ISBN: 9781788995405Supplemental Content