Flask Tutorial
About Lesson

In this Flask lesson we are going to learn about Flask URL Helper Function – Flask url_for(), so in any application when you have more than one routes, you need to include the route links for connecting of different pages, so the user can easily navigate to different pages of your application, for example if you have a navigation bar in your web application you can simply add your Dynamic URL’s in their. there are two ways that you can add this kind of functionality in your Flask Application, the first way is that you can add directly your URL link’s in your templates. but it is not the recommended way, if you have simple routes than you can add directly your URL link’s. but for dynamic and complex routes it is not good idea. the second way is using the helper function, Flask provides the url_for() helper function, which generates URLs from the information stored in the application’s URL map.

 

 

If you remember we have a navbar in our base.html file and we have added our simple routing, now we want to change that with url_for(). it is one of the recommended way of adding URL Link’s. this function takes the view function name (or endpoint name for routes defined with app.add_url_route()) as its single argument and returns its URL. 

 

 

templates/base.html

 

 

templates/index.html

 

 

 

templates/contact.html

 

 

templates/about.html

 

 

 

templates/404.html

 

 

 

templates/500.html

 

 

 

Run your flask application and this is the result.

Flask URL Helper Function - Flask url_for()
Flask URL Helper Function – Flask url_for()