Discover how a simple decorator can turn messy URL checks into clean, automatic routing!
Why Route decorator (@app.route) in Flask? - Purpose & Use Cases
Imagine building a website where you have to manually check the URL path for every request and then decide which function to run.
You write long if-else chains to match URLs like '/home', '/about', or '/contact'.
This manual URL checking is slow, messy, and easy to break.
Adding new pages means changing many parts of your code, increasing bugs and confusion.
The @app.route decorator lets you link a URL directly to a function.
This means Flask automatically calls the right code when someone visits that URL, keeping your code clean and simple.
if path == '/home': return home() elif path == '/about': return about() else: return not_found()
@app.route('/home') def home(): return 'Welcome Home!' @app.route('/about') def about(): return 'About Us'
You can easily add or change website pages by just writing functions with decorators, making your app organized and scalable.
When you visit a blog, each post has its own URL. Using @app.route, the blog app knows exactly which post to show without messy code.
Manual URL handling is complicated and error-prone.
@app.route links URLs to functions cleanly.
This makes web apps easier to build, read, and maintain.