Reputation: 41
I want make custom message web app, which users can send messages each other(not chat, just sending message). I've message model and message form, but i don't know about views and templates looks like. I can't found any tutorial or something about this, most of i found just libraries or something packages. Then, how about this views and templates?
here my models.py:
from django.contrib.auth import get_user_model
class Message(models.Model):
sender = models.ForeignKey(get_user_model(), related_name="sender")
receiver = models.ManyToManyField(get_user_model(), related_name="receiver")
message_file = models.FileField(upload_to='mails/')
timestamp = models.DateTimeField(auto_now_add=True)
unread = models.BooleanField(default = True)
forms.py
from django import forms
from .models import Message
class MessageForm(forms.ModelForm):
class Meta:
model = Message
fields =[
'sender',
'receiver',
'message_file',
]
Thanks in adnvance
Upvotes: 1
Views: 3210
Reputation: 7049
Instead of using a form, I will give you a simple example using a more basic approach. What you will want is something along the lines of this:
views.py
class SendMessageView(View):
def post(self, request, *args, **kwargs):
receiver_id = request.POST.get('receiver_id', '')
file = request.FILES.get('file', '')
Message.objects.create(sender=request.user, receiver__id=receiver_id, message_file=file)
return render(request, 'success.html', {})
urls.py
path('message/send/', views.SendMessageView.as_view(), name="send_message")
html
<form action="{% url 'send_message' %}" method="post">
{% csrf_token %}
<input type="hidden" name="receiver_id" value="{{ receiver.id }}" />
<input type="file" name="file" />
</form>
Upvotes: 2