Integrate Django flash messages into Inertia page props#96
Open
benaduo wants to merge 1 commit intoinertiajs:mainfrom
Open
Integrate Django flash messages into Inertia page props#96benaduo wants to merge 1 commit intoinertiajs:mainfrom
benaduo wants to merge 1 commit intoinertiajs:mainfrom
Conversation
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)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Inertia response cycle so messages are automatically available
as page props on the frontend
permanent message loss on partial page requests
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. SinceInertia 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: Importget_messagesand add flash messages toprops inside
build_props(), after the partial render filtering stepinertia/tests/testapp/views.py: Addmessages_testandno_messages_testviews for testinginertia/tests/testapp/urls.py: Register the two new test URLsinertia/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: