我正在为我的网站创建一个聊天框。按照当前的实现方式,我将遍历当前的好友列表,并且anchor标签触发该特定用户的模式。模态对于所有用户都是通用的,因为它仅更改内部数据。 现在,我已经使用jquery来获取消息历史记录(从模型对象),并且显示的是模式主体。 我可以单击不同的用户并正确查看其各自模型中的消息。但是,当我尝试提交表单以发送另一条消息时,它将被添加到第一位用户的消息框中。朋友列表中的所有用户都在发生这种情况。
我如何触发表单将其发布到正确的用户模型中。
模板
{% for friend in friends_list %}
<a href="" data-toggle="modal" data-target="#chatbox" data-whatever="{{friend.to_user}}"><li style="padding:10px">{{friend.to_user.usercreation.fname}} {{friend.to_user.usercreation.lname}} <i style ="color:green;font-size:0.65rem;text-align:justify;float:right;margin-top:8.5px;" class="fa fa-circle" aria-hidden="true"></i></li></a>
<form action="{% url 'usercreation:addmessage' friend.to_user.usercreation.pk %}" method="post">
{% csrf_token %}
<div class="modal fade" id="chatbox" tabindex="-1" role="dialog">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header text-center">
<h6 class="modal-title w-100">BlogChat</h6>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<pre><div class="modal-body"></div></pre>
<div class="modal-footer">
<input style="width:330px;height:40px" type="text" name="addmessage" value="" placeholder="Type..">
<button style="float:right;" type="submit" class="btn btn-primary">Send message</button>
</div>
</div>
</div>
</div>
</form>
{% endfor %}
JQUERY:
<script type="text/javascript">
$('#chatbox').on('show.bs.modal', function (event) {
var button = $(event.relatedTarget)
var recipient = button.data('whatever')
attributes
$.ajax({
url: "{% url 'fetcher' %}",
data: {
'search': recipient
},
dataType: 'json',
success: function (data) {
list = data.list;
$('#chatbox').find('.modal-body').text(list)
}
});
var modal = $(this)
modal.find('.modal-title').text('Chat with ' + recipient)
})
</script>
观看次数:
def fetcher(request):
if request.is_ajax():
name = User.objects.get(username=request.GET.get('search', None))
b = ChatMessage.objects.get(user1 = request.user,user2 = name)
print(b.message)
data = {
'list': b.message,
}
print(data)
return JsonResponse(data)
def addmessage(request,pk):
if request.method=='POST':
print(request.user)
print(User.objects.get(username=UserCreation.objects.get(pk=pk)))
obj = ChatMessage.objects.get(user1 = request.user , user2 = User.objects.get(username=UserCreation.objects.get(pk=pk)) )
obj2 = ChatMessage.objects.get(user2 = request.user , user1 = User.objects.get(username=UserCreation.objects.get(pk=pk)) )
name = request.POST.get('addmessage')
obj.message += ('\n'+str(request.user)+': '+name)
obj2.message += ('\n'+str(request.user)+': '+name)
obj.save()
obj2.save()
return HttpResponseRedirect(reverse('usercreation:profile',args=[request.user.usercreation.pk]))
型号:
class ChatMessage(models.Model):
user1 = models.ForeignKey(User, on_delete=models.CASCADE ,related_name="participant1")
user2 = models.ForeignKey(User, on_delete=models.CASCADE , related_name="participant2")
message = models.TextField(default="")
date = models.DateTimeField(auto_now=True, db_index=True)