Flask Tutorial – Creating Dynamic URL in Flask

In this Flask Tutorial we are going to talk about Creating Dynamic URL in Flask,

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 template. 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 url_for() helper function, Flask provides the url_for()

helper function, which generates URLs from the information stored in the application’s

URL map.

 

 

 

If you are interested in Web Development with Django Framework, than you can read the

complete articles in this link,  Django Web Development Tutorials.

 

 

 

 

Adding Directly URL Link’s

In this way you can simply add your URL Link’s, in this article we are using our previous

articles source code. we are going to add our URL Link’s in our base.html, because our

navbar is located in the base.html.

 

 

 

So first of all this is our app.py file.

 

 

 

 

templates/base.html

 

 

 

 

So the above code is our base.html, and we have just added directly our contact and home link in our navigation like this .

 

 

 

 

templates/index.html

 

 

 

 

templates/contact.html

 

 

 

These are our Custom error pages in flask that we have already created, you can read this

article for Flask Custom Error Page.

 

 

templates/404.html

 

 

 

 

 

templates/500.html

 

 

 

 

Now if you run your Flask Application, and after that click on the contact, you can see

that we are navigating to the contact page. 

 

Flask Tutorial - Creating Dynamic URL in Flask
Flask Tutorial – Creating Dynamic URL in Flask

 

 

 

 

Adding URL Link’s with url_for()

So as i have said Flask provides url_for() helper function for adding dynamic routes and 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. you need to bring change in your base.html. you can use url_for helper function like this.

 

 

Make sure that you have the same view function name in here.

 

 

After change, this is our base.html.

 

 

templates/base.html

 

 

 

 

If you run the code the result will be the same, but this time we have used url_for()

helper function. also you can use url_for() for dynamic routes, for example you can pass 

the name of your URL function.

 

 

You can use the view function like this.

 

 

And in the url_for() you can do like this.

 

 

 

 

 

 

 

 

Also you can watch my complete 4 hours training on Flask Web Development

Share via
Copy link
Powered by Social Snap