bhylu.com

Advertisements

Django Messages

The messages framework carries short feedback across a redirect. It helps explain a successful change without requiring a query parameter that pretends to be proof of success.

How it works

Messages depend on middleware and a template context processor. The base template renders them as ordinary escaped text in a status paragraph. Consume the storage when testing it, because messages are intended to be displayed and cleared rather than become a permanent audit log. A durable booking record belongs in the database; its one-time confirmation is only interface feedback.

Run in the supplied local Django project

This lesson uses Django 5.2.18 LTS with Python 3.12. Download the source downloader and run node download-project.mjs http://127.0.0.1:8000 with Node 22 while previewing locally. It creates a new bhylu-django-lab folder and refuses to overwrite an existing one. After deployment, the default base is bhylu.com; until then use your local preview URL.

Inside that new folder, create a virtual environment, install requirements.txt and run the complete lesson file shown below. Every lesson creates a fresh in-memory SQLite database and temporary upload storage. It does not edit the interactive lab’s db.sqlite3 or Bhylu’s database. The visible listing below is the lesson’s core code; its complete downloaded file includes the shared bootstrap.

Django does not run in Bhylu’s current browser playground. No Apache, MySQL or global PHP configuration change is needed. See the setup lesson for the full project and commands.

Example

Run .venv\Scripts\python.exe lessons\messages.py from the downloaded project root. Download the complete lesson file; keep it under lessons/. The project’s shared models, forms, views and fixture are prerequisites, not a hidden online runtime.

lessons/messages.py — core code

from django.contrib.auth import get_user_model
from django.contrib.messages import get_messages
client = Client()
client.force_login(get_user_model().objects.get(username="alex"))
response = client.post("/workshops/lamp-care/book/", {"seats":1}, follow=True)
texts = [str(message) for message in get_messages(response.wsgi_request)]
print("Reservation saved in this local lab." in texts)
print(Reservation.objects.count())

Expected result

True
1

The redirected page receives the success message and the database contains the reservation. Testing both distinguishes actual acceptance from an accidentally displayed confirmation string. Error feedback should remain clear about whether anything was saved.

Use Django’s versioned official documentation for exact API contracts and the deployment checklist before a separate release.

Advertisements