render_template() (Flask)

render_template() is the Flask function that loads a Jinja2 template file, fills it with the data you pass as keyword arguments, and returns the finished HTML string ready to send to the browser.

Flask

This is the standard way to return HTML pages from a Flask view. You give it the template's filename (relative to templates/) plus any variables the template needs, and it hands back the rendered markup.

Every keyword argument you pass becomes a variable inside the template. So render_template('user.html', user=u, posts=p) makes user and posts available as {{ user }} and {% for post in posts %}.

If you have a template as a string rather than a file, use render_template_string() instead — but never feed it untrusted user input, as that opens a server-side template injection hole.

Syntax

from flask import render_template
render_template("file.html", key=value)

Examples

Python

from flask import render_template

@app.route("/users")
def users():
    people = ["Ana", "Ben", "Cara"]
    return render_template("users.html", people=people)

Learn it in a lesson

See also

Back to the Dictionary