Flask Routes šŸŽÆ

beginner
11 min

Flask Routes šŸŽÆ

Welcome to the Flask Routes tutorial! In this lesson, we'll dive into one of the essential features of Flask – the routing system. By the end of this tutorial, you'll have a solid understanding of how to create and manage web routes in Flask.

What are Flask Routes? šŸ“

Flask routes are URL endpoints that handle incoming requests. They are the core of any web application built with Flask. By defining routes, we can specify which functions should be executed when a user accesses a specific URL.

Creating Your First Route šŸ’”

Let's start with a simple example. Open your text editor, and create a new Python file called app.py.

python
from flask import Flask app = Flask(__name__) @app.route('/') def home(): return "Welcome to my Flask App!" if __name__ == '__main__': app.run()

Save the file and run it using your terminal. You'll see a message in your browser saying "Welcome to my Flask App!" Congratulations! You've created your first Flask route. šŸŽ‰

šŸ’” Pro Tip: Always import Flask at the beginning of your scripts and create a Flask app object with __name__.

Understanding the @app.route Decorator šŸ“

The @app.route decorator tells Flask to bind the following function to a specific URL. The string you provide as an argument to @app.route is the URL pattern for the route.

In our example, the route is '/', which means the function associated with this decorator will be executed when the user accesses the homepage of the application.

Advanced Routing šŸ’”

Flask routes can be made more specific by using variables within the URL pattern. Here's an example:

python
@app.route('/user/<username>') def user(username): return f"Welcome, {username}!"

In this example, the route '/user/<username>' accepts any string as the URL after /user/. When you access /user/alice, Flask will call the user function and pass the value alice as an argument.

Nested Routes šŸ’”

Nested routes allow you to create a hierarchical structure for your application. Here's an example:

python
@app.route('/') def home(): return "Welcome to my Flask App!" @app.route('/about') def about(): return "About my Flask App."

Now, if you access / in your browser, you'll see "Welcome to my Flask App!" and if you access /about, you'll see "About my Flask App."

Quiz šŸŽÆ

Quick Quiz
Question 1 of 1

What does the `@app.route` decorator do?

Quick Quiz
Question 1 of 1

How can you create a nested route in Flask?

Practical Application šŸ’”

Now that you've learned the basics of Flask routing, let's create a simple to-do list application as a practical example.

python
from flask import Flask, request, render_template, redirect, url_for app = Flask(__name__) tasks = [] @app.route('/') def index(): return render_template('index.html', tasks=tasks) @app.route('/add', methods=['POST']) def add_task(): task = request.form['task'] tasks.append(task) return redirect(url_for('index')) @app.route('/delete/<int:index>') def delete_task(index): del tasks[index] return redirect(url_for('index')) @app.route('/edit/<int:index>') def edit_task(index): return render_template('edit.html', task=tasks[index]) @app.route('/save_edit/<int:index>', methods=['POST']) def save_edit(index): tasks[index] = request.form['task'] return redirect(url_for('index')) if __name__ == '__main__': app.run()

In this example, we have created routes for displaying tasks, adding new tasks, deleting tasks, and editing tasks.

Quick Quiz
Question 1 of 1

What is the purpose of the `render_template` function in the example above?

That's all for our Flask Routes tutorial! Now you have the foundational knowledge to create and manage web routes in Flask. Happy coding! šŸ‘©ā€šŸ’»šŸ‘Øā€šŸ’»