0

I would like to store the last messages to get a chance for the user to check it again.

How can I save it, after show? before show? Do I need to implement a db model?

Paolo Vezzola
  • 105
  • 1
  • 5
  • 1
    Yes you must use a db model. But use a light NoSql like db (redis, mongo, ...) – Rvector Sep 07 '21 at 21:59
  • Actually I have not a huge amount of messages, I still with postgres. My point is how can I intercept all the messages, before or after those are shown? – Paolo Vezzola Sep 07 '21 at 22:15
  • Create a new model to store it in postgres. it's not a big deal since you will store only the 100 news messages. – Rvector Sep 07 '21 at 22:17

2 Answers2

0

you can use redis, Redis might come in handy.

Ali Gökkaya
  • 408
  • 6
  • 20
0

I found a nice solution, without using external library. Just with my lovely python!

First I create a function to store the messages into a database:

def save_messages(message):
    db.objects.add(ManyToMany.object.create(message))
    return redirect('message_saved')

and then I put the function to run anytime a message is being sent into contrib messages itself

MYPROJECT\venv\Lib\site-packages\django\contrib\messages\api.py

def add_message(request, level, message, extra_tags='', fail_silently=False):
"""
Attempt to add a message to the request using the 'messages' app.
"""
try:
    messages = request._messages
except AttributeError:
    if not hasattr(request, 'META'):
        raise TypeError(
            "add_message() argument must be an HttpRequest object, not "
            "'%s'." % request.__class__.__name__
        )
    if not fail_silently:
        raise MessageFailure(
            'You cannot add messages without installing '
            'django.contrib.messages.middleware.MessageMiddleware'
        )
else:
    from WALLAWALLA import save_messages
    save_messages(message) 
    return messages.add(level, message, extra_tags)

and this really works great

Paolo Vezzola
  • 105
  • 1
  • 5