1.

Explain the url_for() function and why would you want to hard-code URLs into your templates instead of utilizing the URL reversing function url_for()?

Answer»

We use the url_for() function to create a URL for a given function. It takes the function's name as the first parameter, as well as any number of keyword arguments, each of which corresponds to a variable component of the URL rule. As query parameters, UNKNOWN variable bits are appended to the URL.

  • When compared to hard-coding URLs, reversing is typically more descriptive.
  • Instead of remembering to manually change hard-coded URLs, you can modify all of your URLs at once.
  • The ESCAPING of special characters is handled transparently during URL creation.
  • The routes generated are always absolute, avoiding unexpected BEHAVIOR in browsers DUE to relative paths.
  • If your application is in a directory other than the URL root, such as /myapplication instead of /, url_for() will take care of it.


Discussion

No Comment Found