Flask Templates – Flask Tutorial

In this Flask Tutorial, we want to talk about Flask Templates, we know that Flask is one of the most popular web frameworks in Python. and template engine is one of the best features that Flask has, templates allows users to build dynamic web pages that can respond to user input and display data from a database or other sources. in this article we want to talk about Flask templates and how they can be used for creating dynamic web pages using Python.

 

 

What are Flask Templates ?

Flask template is a file that contain HTML, CSS and Python code. The Python code is enclosed in special tags called template tags, and it tells Flask how to render the HTML code dynamically. these tags allows you to insert dynamic content into your HTML pages, such as data from a database, user input or other sources.

Flask templates use a syntax called Jinja2, it is a templating language that supports different features like loops, conditional statements and filters.

 

 

How to Create Flask Templates ?

For creating a Flask template, we need to create a new file with .html extension and save it in a templates folder in our Flask project directory. for example, if we have a Flask application in the name of myapp, then we can create another folder in their at name of templates and inside the templates we are going to create our index.html file.

 

 

This is our index.html file, in this example, we have a template file that renders a simple HTML page, and we have some dynamic content like a title and heading. we also have a loop that iterates over a list of items and displays them as a bulleted list.

 

 

Now we need to create our app.py file, because we need to render our HTML file.

 

 

 

Run the code and go to http://127.0.0.1:5000/ URL, this will be the result

Flask Templates - Flask Tutorial
Flask Templates – Flask Tutorial

 

 

Learn More

 

Leave a Comment