Search code examples
pythonajaxdjangodjango-users

How to make django messages StackOverflow style?


I'd like to use Django's Messages module, however, I would like my messages to persist until the user clicks an X next to the message as opposed to having messages disappear as soon as the user reloads the page.

I am stumped with two issues: How do I make the messages' context processor not delete the messages once they are accessed? How do I later on delete the message from the DB explicitly once the user clicks on the "remove" button (which invokes an ajax call)?

Thanks!


Solution

  • In your case django.contrib.messages won't bring you anywhere good. It's a message system inspired by RoR flash system, where messages aren't supposed to stay around

    You should create your own messaging system (django-persistent-messages maybe?) that would save messages for registered users in database.

    • It's a fairly trivial task to implement
    • a model with a foreign key on User
    • a context processor to have the messages available in the templates
    • a view to consume a message
    • maybe a helper function to create the messages

    Don't forget to make it available to others if you do so =)