flask.blog
Flask Templating Explained: Static vs Dynamic Templates (Beginner-Friendly Guide) Uploaded: Nov. 29 2025 | Modified: Nov. 29 2025

Flask Templating Explained: Static vs Dynamic Templates (Beginner-Friendly Guide)

When building web applications with Flask, you’ll often need to display HTML pages to users. Flask makes this easy using templates, which can be either static or dynamic. Understanding the difference between the two is essential for new Flask developers because it affects how your pages behave, how data is rendered, and how your app scales.

In this beginner-friendly guide, you’ll learn:

Let’s get started!


What Are Templates in Flask?

Templates in Flask are HTML files stored inside the /templates folder that the backend renders and sends to the browser.

Templates can contain:

Flask uses the Jinja2 templating engine to insert variables, run loops, render conditions, and generate pages dynamically.


What Is a Static Template in Flask?

A static template is an HTML file that does not change based on dynamic data. It contains plain HTML—no Jinja2 logic, no variables, no conditionals.

Static templates are useful for:

Folder Placement

Static templates still go inside the /templates folder, even if they’re not dynamic.


Example: Static Template in Flask

/templates/static.html

<html>
    <body>
        <h1>Static Template</h1>
        <p>This content never changes.</p>
    </body>
</html>

app.py

from flask import Flask, render_template

app = Flask(__name__)

@app.route('/')
def index():
    return render_template('static.html')

if __name__ == '__main__':
    app.run(debug=True)

Correct usage: You should still use render_template() for HTML files. send_from_directory() is meant for files in the /static folder (CSS, JS, images), not templates.


What Is a Dynamic Template in Flask?

A dynamic template is an HTML file that changes based on variables passed from Flask. It uses Jinja2 syntax such as:

Dynamic templates are used for:


Example: Dynamic Template in Flask

/templates/dynamic.html

<html>
    <body>
        <h1>{{ message }} Template</h1>
        <p>This page is rendered using dynamic data.</p>
    </body>
</html>

app.py

from flask import Flask, render_template

app = Flask(__name__)

@app.route('/')
def index():
    return render_template('dynamic.html', message='Dynamic')

if __name__ == '__main__':
    app.run(debug=True)

The output will display:

Dynamic Template

Static vs Dynamic Templates (Quick Comparison)

Feature Static Template Dynamic Template
Contains Jinja2 No Yes
Changes based on Python data No Yes
Good for Simple pages, policies Dashboards, blogs, data-driven pages
Render method render_template() render_template()

FAQs (Frequently Asked Questions) About Templates in Flask

1. Do static templates have to be inside the /templates folder?

Yes. All HTML templates—static or dynamic—must be inside the /templates directory.


2. How is a static template different from a static file?

Static templates are HTML pages. Static files are CSS, JS, images inside the /static folder.


3. Can I return static HTML without using templates?

Yes, but it's not recommended. Flask templates provide cleaner structure.


4. What is Jinja2 used for in Flask?

Jinja2 lets you insert variables, loops, and conditional logic inside HTML.


5. Can I pass multiple variables to a dynamic template?

Yes, for example:

render_template('page.html', title='Home', username='John')

6. Can dynamic templates load data from a database?

Absolutely. That’s one of the main uses of dynamic templates.


7. Do dynamic templates affect performance?

Not significantly. Jinja2 is very fast and efficient for rendering HTML.


8. Can I mix static and dynamic sections in one template?

Yes. A template can have plain HTML and Jinja2 code together.


9. What file extensions does Flask support for templates?

Mostly .html, but .jinja or .jinja2 also work.


10. Do I need to restart Flask whenever I change templates?

No, not in debug mode. Flask auto-reloads updated templates.