
Routing & Controllers: Handling Dynamic Data in URLs
What are Dynamic Routes? Static routes like /about always return the same thing. Dynamic routes change their response based on what's in the URL. This is how real APIs work — /user/1 and /user/2 return different users, not the same response. My 3 Dynamic Routes for Day 4 1. User by ID @app.get("/user/{user_id}") def get_user(user_id: int): return { "user_id": user_id, "message": f"Fetching user with ID {user_id}" } 2. Product by Name @app.get("/product/{product_name}") def get_product(product_name: str): return { "product": product_name, "message": f"Fetching product: {product_name}" } 3. Nested Parameters — User + Order @app.get("/user/{user_id}/order/{order_id}") def get_user_order(user_id: int, order_id: int): return { "user_id": user_id, "order_id": order_id, "message": f"Order {order_id} for user {user_id}" } Why Nested Parameters Matter /user/1/order/42 tells you exactly what resource you're dealing with — user 1's order number 42. This is how e-commerce APIs, banking systems, an
Continue reading on Dev.to Python
Opens in a new tab




