Opened 21 months ago

Closed 16 months ago

Last modified 10 months ago

#20610 closed New feature (fixed)

Add a message level dict to `contrib.messages.context_processors.messages`

Reported by: bmispelon Owned by: nobody
Component: contrib.messages Version: master
Severity: Normal Keywords:
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

It'd be nice if the provided context processor also added a dictionary of message levels to the context.

This would allow something like this:

{% for message in messages %}
    {% if message.level == MESSAGE_LEVELS.SUCCESS %}...{% endif %}
    ...
{% endfor %}

Currently, one has to pass the message levels manually into the context, or do something like this, which is brittle:

{% if 'success' i nmessage.tags %}...{% endif %}

There should not be any backwards-compatibility issue and the change is rather minimal.
I've attached a proof-of-concept patch. If there's interest, I'd be willing to polish it up (mostly adding tests and documentation).

Attachments (1)

message-level-dict.patch (804 bytes) - added by bmispelon 21 months ago.

Download all attachments as: .zip

Change History (7)

Changed 21 months ago by bmispelon

comment:1 Changed 21 months ago by aaugustin

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

Technically, it's backwards incompatible for people who have "MESSAGE_LEVELS" in their context. That's minor, but it should be mentioned in the release notes.

comment:2 Changed 19 months ago by timo

  • Has patch set
  • Needs documentation set
  • Needs tests set
  • Triage Stage changed from Unreviewed to Accepted
  • Version set to master

comment:3 Changed 16 months ago by bmispelon

  • Needs documentation unset
  • Needs tests unset

Here's a pull request with tests and documentation: https://github.com/django/django/pull/1822

comment:4 Changed 16 months ago by timo

  • Triage Stage changed from Accepted to Ready for checkin

comment:5 Changed 16 months ago by Baptiste Mispelon <bmispelon@…>

  • Resolution set to fixed
  • Status changed from new to closed

In 9fde42a69a56cb6c8cdfd67b07c67a1b97638f12:

Fixed #20610: Added a message level dict to contrib.message context processor.

comment:6 Changed 10 months ago by Claude Paroz <claude@…>

In 79e4a189f3b17b5a7426f889c4412126eb169256:

[1.7.x] Updated the messages context processor docs

Refs #20610.
Backport of 7951f125e frmo master.

Note: See TracTickets for help on using tickets.
Back to Top