Opened 3 weeks ago
Last modified 3 weeks ago
#37070 assigned New feature
Add .clear_messages() method to django.contrib.messages
| Reported by: | benjaoming | Owned by: | Dinesh Thumma |
|---|---|---|---|
| Component: | contrib.messages | Version: | dev |
| Severity: | Normal | Keywords: | |
| Cc: | Triage Stage: | Accepted | |
| Has patch: | no | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | no | UI/UX: | no |
Description (last modified by )
Disclaimer: This is really a minor issue 😊
We can do:
from django.contrib.messages import get_messages
And then we can call get_messages() - and if we iterate over them, they will be cleared.
But we can't explicitly clear them because this method doesn't exist:
from django.contrib.messages import clear_messages
For me, the use-case was test-related: Writing an e2e test case that calls some views and at each step verified messages, then cleared them.
Not sure if adding the method would make people use the messages framework wrongly, but I think it makes sense to note that ADDING clear_messages to the mix might make some people think you need to call it... which in most cases, you don't, and therefore the documentation needs to be clear (pun intended).
Forum discussion: https://forum.djangoproject.com/t/why-isnt-it-easier-to-clear-messages/5727/7
Change History (4)
comment:1 by , 3 weeks ago
| Description: | modified (diff) |
|---|---|
| Version: | → dev |
comment:2 by , 3 weeks ago
| Triage Stage: | Unreviewed → Accepted |
|---|
comment:3 by , 3 weeks ago
My only request would be to name this consistently with how we reset the queries:
>>> from django.db import connection, reset_queries >>> connection.queries [] >>> reset_queries() >>>
Therefore I think the function should be called reset_messages().
comment:4 by , 3 weeks ago
| Owner: | set to |
|---|---|
| Status: | new → assigned |
Accepting without requesting this be added to the new features board as this is based off an existing older discussion with already a good consensus that this is useful and a small addition