Creating URLs in Flask

Flask can generate URLs using the url_for() function of the flask package. Hardcoding URLs in the templates and view functions is a bad practice. Suppose, we want to re-structure URLs of our blog from /<id>/<post-title>/ to /<id>/post/<post-title>/. If we had hardcoded URLs in our templates and view functions then we would have to manually visit every template and view function to make the changes. However, with the url_for() function changes like these could be done in a snap.

The url_for() function accepts the endpoint and returns the URL as a string. Recall that endpoint refers to the unique name given to URL and most of the time it is the name of the view function. At this point, main2.py contains a root ( / ) route defined as follows:

To generate root URL call url_for() as url_for('index'). The output would be '/'. The following shell session demonstrates how to use url_for() inside the console.

Notice that we are first creating a request context (and thus application context implicitly). Trying to use url_for() inside the console without the request context will result in an error. You can learn more about application and request context here.

If url_for() couldn’t create URL, it will throw BuildError exception.

To generate the absolute URLs pass _external=True to the url_for() as follows:

Rather than hardcoding URLs in the redirect() function you should always use url_for() to generate URLs. For example:

To generate URLs for dynamic routes pass dynamic parts as keyword arguments. For example:

The extra number of keywords arguments passed to the url_for() function will be appended to the URL as a query string.

The url_for() is one of those few functions which are available to you inside the template. To generate URLs inside templates simply call url_for() inside the double curly braces {{ ... }}, as follows:

Output:

Leave a Comment