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.
