StaffCoder
Django
DjangoDjango 5.2Python 3.13
18 modules · 72 challengesSign up
Module 14 · Sessions & Messages

Flash Message Feedback

mediumDjango Messages FrameworkTemplates—No data yet

Problem statement

In this Django project, implement the feedback the reading list never gives. Saving a title, deleting a bookmark and submitting an empty title all end the same way - a redirect back to a list that comes back looking exactly as it did before - so a save that landed cannot be told from a save that was refused, and the only way to find out whether a delete fired is to count the rows. Queue a message at the level that fits what happened, and render the queue once, where every page can show it.

Requirements

Implement:

  • the feedback in bookmark_save and bookmark_delete in app/views.py
  • the message block in app/templates/app/base.html

The following files are already implemented and provided: app/models.py, app/urls.py and app/templates/app/bookmark_list.html. Bookmark has a title and a url and nothing else: the question is about what the user is told, not about what is stored. The routes are given: / (home) and /bookmarks/ (bookmark-list) answer GET, and /bookmarks/<pk>/save/ (bookmark-save) and /bookmarks/<pk>/delete/ (bookmark-delete) take a POST, the save carrying a title field. django.contrib.messages is installed - its app, its middleware and its context processor are all in project/settings.py already - so messages is a name every template can read. bookmark_list renders _bookmark_list.html_, which extends base.html and posts to the two action routes. Both actions already do their work and both already redirect("bookmark-list"), and the require_POST decorators and the lookups are given too.

1. The message block in the base template

  • app/templates/app/base.html loops over messages above {% block content %}, rendering one element per message inside one element carrying the class messages.
  • Each message element carries the class message and, alongside it, that message's own level_tag - success, info, warning or error - and no other level's tag. The tags themselves are yours; the classes are not.
  • A page with nothing queued renders no message block at all, not an empty one. {% if messages %} asks whether the queue is empty without consuming it.
  • The loop belongs in the base template, once, rather than in _bookmark_list.html_: it is the page every view inherits.

2. POST /bookmarks/<pk>/save/ with a title

  • Reports exactly this text at level SUCCESS: ``text Bookmark saved. ``

3. POST /bookmarks/<pk>/save/ with a blank title

  • A title that is empty or only whitespace is not a save. It reports exactly this text at level ERROR: ``text Bookmark not saved: the title cannot be blank. ``
  • The bookmark is left exactly as it was: the old title is still in the database and still on the page.

4. POST /bookmarks/<pk>/delete/

  • Queues two messages, in this order, each at its own level: ``text WARNING Bookmark deleted. INFO Deleted bookmarks cannot be restored. ``
  • Both render, in that order, inside the single message block.

5. Each message is shown once

  • A message appears on the page the action redirects to, and is gone from the page after that, along with the block that held it.
  • Iterating the queue in the template is what does this: rendering the loop marks the storage used and the response clears it. A template that never loops over messages leaves them queued, and they turn up on whatever page the user opens next.
  • Opening the list queues nothing of its own, and a later action shows only its own messages.

Notes

  • Only app/views.py and app/templates/app/base.html change.
  • messages.success, messages.info, messages.warning and messages.error are shorthands for messages.add_message(request, messages.SUCCESS, ...) and friends, and a view may queue as many as it likes: they come back in the order they were queued.
  • A message carries message.level (the integer) and message.level_tag (the lower-case name), so the styling follows the level rather than the text.
  • Use django.contrib.messages; do not hand-roll feedback in request.session or a query string, and do not keep a copy of a message anywhere.
  • The redirect("bookmark-list") at the end of each action is given and stays: a POST that redirects has no page of its own to write on, which is why the note is left for the next request to pick up.
  • Do not change bookmark_list, the models, the routes or the settings.

More in Sessions & Messages

See the module
Session Shopping CartAnonymous visitors lose their cart on every page change because nothing persists between requests. Keep the cart in the session, mutate it correctly on add, update and remove with the session flagged as modified, and work the totals out server-side from the price list.—No data yetmediumSession Preferences And ExpiryA visitor's theme and page-size choices reset on every page because no view reads or writes the session, and abandoned sessions keep the two-week global default instead of the site's policy. Store both preferences under the documented keys with their defaults, apply them to the rendered page, and set each visitor's session expiry per policy including the forget-at-browser-close toggle.—No data yetmediumGuest Checkout FlowA guest checkout walks from the cart to the details to the confirmation, but nothing carries between the steps, no step says what just happened, and an abandoned flow sits in the session store for a fortnight. Build the three steps on session state with a message at every transition, keep the entries through back-navigation, and clear only the flow's own keys on completion under the documented expiry policy.—No data yetmedium

We use cookies that are necessary to run Staffcoder, and optional analytics cookies to see which pages get used. Analytics stays off unless you accept. Read our Cookie Policy.