KOSUNI KOREA
Korean News in English
Technology

Python Tutorial: Building Web Pages with HTML Templates (Jinja2)

Published October 17, 2026 · 1 views
Python Tutorial: Building Web Pages with HTML Templates (Jinja2)

[Python 강좌] Python Flask 블로그 만들기, 지금부터 함께 시작합니다.

Let's Build a Real Website

In the previous lesson, we learned how to display text in Flask.

For example, writing something like this would only show plain text in a browser:

@app.route("/")
def home():
    return "Hello."

This works for learning purposes, but real websites aren't built this way.

What we use daily—blogs, online stores, community sites—are all structured using HTML, a language that defines web page layouts.

In this lesson, we’ll create actual website interfaces in Flask using HTML files.


What Are HTML Templates?

An HTML template is a pre-made file that structures the visual layout of a webpage.

Flask uses Jinja2, a built-in templating engine, to manage these templates.

With Jinja2, you can easily insert Python data into HTML and reuse layouts across multiple pages.

In short, Jinja2 connects Python code with HTML.


Create a Templates Folder

Flask looks for HTML files in a specific folder.

Create a templates folder inside your project directory like this:

python-flask-blog/
│
├── app.py
├── templates/
│   └── index.html
└── venv/

All future HTML files will go into this templates folder.


Create Your First HTML File

Inside the templates folder, create a file called index.html.

Write simple HTML like this:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Python Flask Blog</title>
</head>
<body>

<h1>Python Flask Blog</h1>

<p>First HTML page.</p>

</body>
</html>

It’s simple now, but we’ll gradually add features to improve it.


How to Display HTML in Flask

After creating an HTML file, you need to load it in Flask.

First, import render_template:

from flask import Flask, render_template

Then update your homepage route:

@app.route("/")
def home():
    return render_template("index.html")

Now Flask will display the index.html file instead of plain text.


What Is render_template?

The render_template() function in Flask is used to output HTML files.

For example:

return render_template("index.html")

Flask will look for and execute this file:

templates/index.html

You only need to specify the filename, and Flask automatically finds it in the templates folder.


Different Roles: HTML vs. Python

New web developers often confuse HTML and Python roles.

Here’s a quick breakdown:

Technology

Role

HTML

Structures the page layout

CSS

Styles and designs the interface

Python (Flask)

Handles functionality

MySQL

Saves data

All four work together to create a full website.

This lesson focuses on HTML, the part that structures your page layout.


Display Python Data in HTML with Jinja2

Jinja2 lets you display Python data directly in HTML.

For example, in Python:

@app.route("/")
def home():
    title = "Python Flask Blog"
    return render_template("index.html", title=title)

In HTML, display it like this:

<h1>{{ title }}</h1>

The browser will show:

Python Flask Blog

Using the {{ }} syntax, you can insert Python values into HTML.


How This Will Be Used Moving Forward

This technique will be used repeatedly in future lessons.

For example, to display a post title:

{{ post.title }}

Date and author would look like this:

{{ post.created_at }}
{{ post.author }}

Most blog pages will use this method to display content.


Your Project Structure Is Taking Shape

Your project now looks like this:

python-flask-blog/
│
├── app.py
├── templates/
│   └── index.html
└── venv/

In the next lesson, we’ll add CSS to make it look like a real blog.


Key Takeaways for Today

Remember these main points:

  • HTML files go in the templates folder.

  • Use render_template() to display HTML in Flask.

  • Jinja2 connects Python and HTML.

  • Use {{ }} syntax to insert Python data into HTML.

If you understand these four points, you’ll easily follow most future screen development.


In Closing

Today we learned how to display web pages in Flask using HTML templates and the basics of Jinja2 templating.

Now you can move beyond simple text output to structure actual website layouts with HTML.

In the next lesson, we’ll apply CSS to make a clean, visually appealing blog design. Even small changes will transform your site’s look and feel—let’s see it together!

FAQ

What is a template engine in web development?

A template engine like Jinja2 allows developers to insert dynamic content into HTML files, connecting Python logic with the visual layout of a webpage.

Korean Source

This article is an English localization of a Korean-language report from BlogScore. Original: [Python 강좌] 4강. HTML 템플릿(Jinja2)으로 화면 만들기