Flask 3 cheatsheet
Flask 3: Templates, sessions and config
7 common Flask 3 tasks.
Render a template
Render templates/index.html with variables. Jinja escapes values by default.
return render_template("index.html", title="Home", posts=posts)
Loop in a template
Use Jinja control structures in the template.
{% for post in posts %}
<h2>{{ post.title }}</h2>
{% else %}
<p>No posts yet.</p>
{% endfor %}
Share a layout
Define blocks in a base template and fill them in child templates.
{# child.html #}
{% extends "base.html" %}
{% block content %}
<h1>Hello</h1>
{% endblock %}
Link to a static file
Build the URL of a file in the static folder.
<link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}">
Remember a user between requests
Store values in the signed cookie session. It needs a secret key.
app.secret_key = os.environ["SECRET_KEY"]
session["user_id"] = user.id