Skip to content

Integrate Django flash messages into Inertia page props#96

Open
benaduo wants to merge 1 commit intoinertiajs:mainfrom
benaduo:fix/flash-messages
Open

Integrate Django flash messages into Inertia page props#96
benaduo wants to merge 1 commit intoinertiajs:mainfrom
benaduo:fix/flash-messages

Conversation

@benaduo
Copy link
Copy Markdown

@benaduo benaduo commented Feb 24, 2026

Summary

  • Integrated Django's built-in flash messages framework into the
    Inertia response cycle so messages are automatically available
    as page props on the frontend
  • Messages are added after partial render filtering to prevent
    permanent message loss on partial page requests
  • Messages key is omitted entirely when no messages exist, keeping
    props clean

Motivation

Fixes Issue #38

In a standard Django app, flash messages set via messages.success(),
messages.error() etc. are consumed by the template layer. Since
Inertia bypasses Django templates and communicates entirely through
JSON props, these messages were silently lost — they were never
included in the Inertia response, so the frontend had no way to
display them.

Changes

  • inertia/http.py: Import get_messages and add flash messages to
    props inside build_props(), after the partial render filtering step
  • inertia/tests/testapp/views.py: Add messages_test and
    no_messages_test views for testing
  • inertia/tests/testapp/urls.py: Register the two new test URLs
  • inertia/tests/test_messages.py: 3 tests covering message inclusion,
    empty props behavior, and message consumption after render

Frontend Usage

Once merged, flash messages are available in props automatically:

// React / Vue / Svelte
const { messages } = usePage().props

// Each message has:
// { level: "success" | "error" | "warning" | "info", message: "..." }

Django's built-in messages framework had no way to reach the frontend
in an Inertia application. Since Inertia bypasses Django templates
entirely and communicates via JSON props, flash messages set in views
using messages.success(), messages.error() etc. were silently lost —
never making it to the frontend at all.

This commit integrates Django's messages framework into the Inertia
response cycle by automatically including any pending flash messages
in the page props on every render. Messages are added after the
partial render filtering step to ensure they are never accidentally
dropped during partial page requests, which would cause them to be
consumed and permanently lost.

The frontend receives messages under a `messages` key in props, with
each message containing its level tag and text content. If there are
no messages, the key is omitted entirely to keep props clean.

Changes:
- Import `get_messages` from `django.contrib.messages` in http.py
- Add flash messages to props in `build_props()` after partial render
  filtering to prevent message loss on partial requests
- Add test views and URLs for flash message scenarios
- Add `test_messages.py` with 3 tests covering message inclusion,
  empty message handling, and message consumption behavior

Fixes Issue inertiajs#38

Author: Benjamin Aduo (@benaduo)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant