What is the purpose of Flask's `url_for` function?

Instruction: Explain how `url_for` works and provide examples of its usage.

Context: This question checks the candidate's understanding of generating URLs in Flask applications, emphasizing the benefits of using `url_for` for maintainability.

Official answer available

Preview the opening of the answer, then unlock the full walkthrough.

To directly address the question, the purpose of Flask's url_for function is to dynamically generate URLs for a specific function or resource within a Flask application. This is particularly useful because it abstracts the actual URL paths, allowing developers to modify routes without affecting the links in templates and redirect functions.

For instance, in a Flask application, if we have a route defined as follows to show a user's profile:...

Related Questions