In this post we will learn about routing in flask. Its pretty easy.
To create routes for your pages like about , contact and so on, we use this kind of routing when we have a permanent route.
return "Contact Us Page"
return "About Us Page"
Creating fixed URLs is so damn simple. You might have spotted that in contact us page there was no trailing slash but in contact us we used it. We can use both, but keep in mind when we don’t use trailing slash it will give a
404 error to people who visit the traling slash page . For Example it will give a
404 when a person visits
\contact-us\ but will server page on
If you use the one URL pattern with trailing backlash like
\about-us\ it will redirect users from
\about-us\ and thus preventing mess.
In this section we will learn about dynamic routing. Dynamic routing means getting dynamic data in URL and then using it.
In Flask you can have use following converters to convert dynamic input from the URL.
- path ( It is simple a string but also accepts shashles in the URL)
You may get data from URL without this parameters but is recommended to use.
Let’s now see some examples:
In above you may have seen that we pass the varible used in dynamic URL to the function and its how it is done, and previously I have talked about converters lets see how to use them,
It will convert the given input in string and will pass it to the function. You may try and use other converters on your own.