0
0
Flaskframework~30 mins

Render_template function in Flask - Mini Project: Build & Apply

Choose your learning style9 modes available
Using the render_template Function in Flask
📖 Scenario: You are building a simple Flask web app that shows a welcome message on the homepage.
🎯 Goal: Create a Flask app that uses the render_template function to display an HTML page with a welcome message.
📋 What You'll Learn
Create a Flask app instance named app
Create a route for the homepage at /
Use render_template to render an HTML file named index.html
Pass a variable named message with the value 'Welcome to Flask!' to the template
💡 Why This Matters
🌍 Real World
Web developers use Flask and render_template to build dynamic websites that show different content based on user requests.
💼 Career
Knowing how to use render_template is essential for backend web development with Flask, a popular Python web framework.
Progress0 / 4 steps
1
Set up the Flask app and import render_template
Write code to import Flask and render_template from flask. Then create a Flask app instance called app.
Flask
Need a hint?

Use from flask import Flask, render_template and then app = Flask(__name__).

2
Create a route for the homepage
Add a route decorator @app.route('/') and define a function named home that will handle requests to the homepage.
Flask
Need a hint?

Use @app.route('/') above a function named home.

3
Use render_template to render index.html with a message
Inside the home function, return render_template('index.html', message='Welcome to Flask!') to send the message to the template.
Flask
Need a hint?

Use return render_template('index.html', message='Welcome to Flask!') inside the home function.

4
Create the index.html template file
Create an HTML file named index.html in a folder named templates. Inside it, use {{ message }} to display the passed message inside an <h1> tag.
Flask
Need a hint?

Put <h1>{{ message }}</h1> inside index.html in the templates folder.