Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a user to user message system using Django?

I am trying to create a private messaging system using Django. It doesn't have to be like a live chat, just have an inbox and be able to respond, simple thing like that. So far, from research I have found two things: django.contrib.messages which doesn't seem to be for private messages, but rather messages from the system to a user. Then I also found django-postman which seems to be what I'm looking for, but there seems to be little documentation on how to use it. So has anybody used anything else to accomplish this? Or am I wrong about django.contrib.messaging? Or is there good documentation on django-postman that I'm missing?

like image 513
programmr Avatar asked Sep 21 '15 04:09

programmr


People also ask

How do I add messages in Django?

Adding a message To add a message, call: from django. contrib import messages messages. add_message(request, messages.INFO, 'Hello world.

How do I use Django message framework?

Import messages from django. contrib at the top of the file then go to the view function where you wish to add the message(s). In this case, it's a contact view that needs a Django success message and a Django error message. Add a success message just before the return redirect ("main:homepage") stating "Message sent."

How do I clear Django messages?

If you need to delete the messages or you don't want to show the messages on a certain template, just iterate over the messages without doing anything in the template file. I added these lines in the template in which the messages should be deleted. Doing this in views is not possible now I guess.


1 Answers

You can create your own custom message app.

Models for Message like app : Class Message():

Class Message(models.Model):
     sender = models.ForeignKey(User, related_name="sender")
     reciever = # almost same as above field, just change the related-name
     msg_content = # text field 
     created_at = # time field

Create a form for this model, use model form .

filter "Inbox" queries in views.py by

Message.objects.filter(reciever=request.user)

filter "Sent Box" queries in views.py by

Message.objects.filter(sender = request.user)
like image 60
ancho Avatar answered Sep 23 '22 18:09

ancho