Skip to content

Flash Messages

Session-backed notifications surfaced on the next page render. Rendered automatically by base.html.j2 — no template changes needed.

Adding a message

from fastpluggy.core.flash import FlashMessage

FlashMessage.add(request, "Saved successfully!", category="success")

The message is plain text (see Template escaping): HTML in it shows as its source. To point at another page, pass the link as its own field. It is printed after the message:

task_url = request.url_for("task_details", task_id=task_id)
FlashMessage.add(
    request,
    "Purge submitted as a background task —",
    link_url=task_url,
    link_label="view progress",
)

link_url is a relative, http or https URL (anything else raises ValueError), and link_url and link_label go together. Since 0.5.3.

Categories

Category Visual Use for
"info" Blue Neutral information
"success" Green Completed actions
"warning" Amber Non-blocking issues
"danger" Red Errors, failures

Typical pattern — POST/Redirect/GET

from fastpluggy.core.tools.fastapi import redirect_to_previous, same_origin_or_403

@router.post("/save", dependencies=[Depends(same_origin_or_403)])
async def save(request: Request, fast_pluggy=Depends(get_fastpluggy)):
    # ... do work ...
    FlashMessage.add(request, "Settings saved!", category="success")
    return redirect_to_previous(request)

The message is stored in the session and displayed on the next GET render, then cleared.

Who may send the POST

A route that changes state is a POST, never a GET, and declares where it may be called from (since 0.4.91):

Dependency Passes Refused (403) Use for
same_origin_or_403 Origin or Referer naming the application's host another host, or neither header a route only the application's pages call (forms and fetch)
not_cross_origin_or_403 the same, a request with neither header, and a browser extension (moz-extension://, chrome-extension://: no website can send that origin) another host a route scripts call too (a JSON API)

Every route of a plugin already has not_cross_origin_or_403 on its writes: the core adds it when it mounts the plugin's router (POST, PUT, PATCH, DELETE; reads and WebSocket connections are not checked). A plugin therefore declares nothing for a JSON API, a webhook or a token-authenticated upload, and only adds same_origin_or_403 to a route its own pages alone call.

A browser sends Origin on every POST, so the pages pass both. The session cookie being SameSite=Lax is not enough on its own: behind a proxy that authenticates the user, or with HTTP basic auth, the browser attaches the credentials whatever site the request came from. The host is compared with the one the request was addressed to, so a reverse proxy must forward Host unchanged. This is not a synchronizer token.

Demo route

The example plugin ships a /flash-demo route (example_flash_demo) that fires all four categories at once — useful for visual testing.