[Fixed]-Send django message from a template file

1đź‘Ť

There are two wrong assumptions here.

Firstly, you can’t do anything “from the template”. Everything that requires user interaction must be done via some kind of request that is processed in a view. If you want that to be transparent to the user, you could use some an Ajax request, but it does still need to be a request.

Secondly, Django’s messaging framework is not suitable for sending messages between users. The messages are stored in the user’s session, and there is no way for one user to modify another user’s session. There are plenty of third-party messaging libraries for Django, or you could roll your own fairly simply.

0đź‘Ť

Yes, you should use the Django messaging framework to add the message in the view, then display it to the user in the template. See the docs here https://docs.djangoproject.com/en/1.10/ref/contrib/messages/

👤Dan Banks

Leave a comment