0
0
Flaskframework~30 mins

Blueprint routes and templates in Flask - Mini Project: Build & Apply

Choose your learning style9 modes available
Blueprint Routes and Templates in Flask
📖 Scenario: You are building a simple Flask web app that has a main page and a user page. To keep your code organized, you want to use a Blueprint for the user-related routes and templates.
🎯 Goal: Create a Flask Blueprint named user_bp with a route /profile that renders a template profile.html. Also, set up the main Flask app to register this Blueprint and render a home page at /.
📋 What You'll Learn
Create a Flask Blueprint called user_bp in a separate module
Define a route /profile inside user_bp that renders profile.html
Create a simple profile.html template with a heading
In the main app, register the user_bp Blueprint
Define a route / in the main app that renders home.html
Create a simple home.html template with a welcome message
💡 Why This Matters
🌍 Real World
Blueprints help organize larger Flask apps by grouping related routes and templates, making code easier to maintain.
💼 Career
Understanding Blueprints is essential for backend web developers working with Flask to build scalable and modular web applications.
Progress0 / 4 steps
1
Create the Blueprint and its route
In a file named user.py, create a Flask Blueprint called user_bp. Define a route /profile inside user_bp that returns render_template('profile.html').
Flask
Need a hint?

Use Blueprint from flask and set template_folder='templates' to locate templates.

2
Create the profile.html template
Create a file named templates/profile.html. Add a simple HTML page with a heading <h1>User Profile</h1>.
Flask
Need a hint?

Use a basic HTML5 structure with a heading inside the body.

3
Set up the main Flask app and register the Blueprint
In app.py, create a Flask app instance. Import and register the user_bp Blueprint. Define a route / that returns render_template('home.html').
Flask
Need a hint?

Import Flask and render_template. Use app.register_blueprint(user_bp) to add the Blueprint.

4
Create the home.html template
Create a file named templates/home.html. Add a simple HTML page with a heading <h1>Welcome to the Home Page</h1>.
Flask
Need a hint?

Use a basic HTML5 page with a heading inside the body.