Routing
The route that we wrote in the previous tutorial, is Static Route. If we want to create a profile page for our users, our web service should generate URLs of their profiles dynamically. We cannot solve this by using static routes. We have to create dynamic routes (variable rules ...)
Simple logic for profile page:
1 2 3 4 |
|
For these dynamic routes, we should write the dynamic URL rules like this:
1 2 3 4 5 6 7 8 9 10 11 |
|
By defining our URL rule within < ... >
, it indicates a variable. They can be type-checked by adding a colon(:
). There are data types that routes can accept:
Variable Rules
string
- (default) accepts any text without a slashint
- accepts positive integersfloat
- accepts positive floating point valuespath
- like string but also accepts slashesuuid
-accepts UUID strings
Variable Rules should be given as the view function's argument.
ProTips
- You can handle multiple routes with a single function by simply stacking additional route decorators above any route!
1 2 3 4 5
@app.route("/") @app.route("/home") @app.route("/something") def home(): return "Hello Bro 👋"