6👍
For the sake of resolution I’m going to mark the method I went with as “The Answer”. Thanks to those who commented.
I went with this:
storage = messages.get_messages(request)
storage.used = True
Because it seemed cleaner, was easier to test, and conformed with the general practices of the rest of the project.
9👍
I had to use 2 of the solutions proposed above toghether as no one alone was enought:
storage = messages.get_messages(request)
for _ in storage:
pass
if len(storage._loaded_messages) == 1:
del storage._loaded_messages[0]
As far as the accepted solution I can loop over the messages several time
and I see that the messages don’t seem to be “consumed”
- [Django]-Django, Models & Forms: replace "This field is required" message
- [Django]-Can i add help text in django model fields
- [Django]-Django Model – choices versus foreign key?
8👍
I like this simpler approach for clearing out the underlying iterator, since I actually wanted to add a new message in the place of a standard Django message.
list(messages.get_messages(request))
- [Django]-ValueError: Related model u'app.model' cannot be resolved
- [Django]-Django: Display Choice Value
- [Django]-UUID as default value in Django model
2👍
If your logout view always redirects to a “logout page”, then you can just change your logout template to hide your messages.
e.g., in template:
{% block extra-header %}
<style type="text/css">
#message-id { display: none; }
</style>
{% endblock %}
It feels a little ‘hacky’ but I think it’s certainly less hacky than your #2.
- [Django]-Django: Get current user in model save
- [Django]-Django self-referential foreign key
- [Django]-How to build a push system in django?
2👍
For me in Django 1.5
and session
message storage accepted method dit not the trick.
I needed to use:
storage = messages.get_messages(request)
for _ in storage:
pass
To get rid of messages from storage.
- [Django]-Advantages to using URLField over TextField?
- [Django]-Why are blank and null distinct options for a django model?
- [Django]-Django – cannot import name 'config' from 'decouple'
0👍
One way of doing the same thing in Django Admin (tested with Django==1.11.6
) is to override response_post_*
.
def response_post_save_change(self, request, obj):
storage = messages.get_messages(request)
storage._loaded_messages = []
return super().response_post_save_change(request, obj)
And if you want to keep only your custom messages (e.g. added in save_model
or any other overridden method) remove the last queued message (which is the one Django ads by default).
def response_post_save_change(self, request, obj):
storage = messages.get_messages(request)
if len(storage._queued_messages) > 1:
del storage._queued_messages[-1]
return super().response_post_save_change(request, obj)
- [Django]-Fabric Import Error: cannot import name 'isMappingType'
- [Django]-Github issues api 401, why? (django)
- [Django]-AngularJS + Django Rest Framework + CORS ( CSRF Cookie not showing up in client )