gpt4 book ai didi

python - 为什么我的私有(private)用户间聊天显示在其他用户的聊天个人资料中?

转载 作者:太空宇宙 更新时间:2023-11-03 15:48:48 24 4
gpt4 key购买 nike

我正在创建一个私有(private)用户与用户的聊天,我遇到一个问题,当一个用户与另一个用户发送消息时,相同的聊天会出现在其他用户的聊天中。

假设用户A联系了用户B,用户C看不到用户A和B之间发送的消息,但是用户A和B可以看到他们在用户C的聊天资料中的讨论,这些讨论与他们无关。

问题:如何才能使其仅在与发件人和收件人有关时显示聊天?

聊天配置文件显示在 URL 页面上,如下所示:/c/<recipient username>/

如下图所示,一切都是正确的,因为 URL 用户名与发件人或收件人用户名匹配。

enter image description here

正如您在另一张图片上看到的,网址用户名与发送或收件人用户名无关。

enter image description here

这是我的模板文件:

{% for user in users %}
{% if user.client == request.user %}
<li style="text-align:left; background:yellow;">
<p>from {{ user.client }} to <strong>{{ user.worker }} </strong> | {{ user.sent_at }}</p>
<p>{{ user.comment }}</p>
</li>
{% else %}
<li style="text-align:right; background:#eaeaea;">
<p>from {{ user.client }} to <strong>{{ user.worker }}</strong> | {{ user.sent_at }}</p>
<p>{{ user.comment }}</p>
</li>
{% endif %}
{% endfor %}

views.py

def comment(request, username):
username = User.objects.get(username=username)
user = userComment.objects.filter(Q(sender=request.user) | Q(recipient=request.user)).order_by('sent_at')

context = {'users': user, 'url_username': username}
return render(request, 'add_comment.html', context)

models.py

class userComment(models.Model):
sender = models.ForeignKey(User, related_name="sender")
recipient = models.ForeignKey(User, blank=True, null=True, related_name="recipient")
sent_at = models.DateTimeField(auto_now_add=True)
comment = models.TextField(max_length=255, null=True)

我不知道这是否与 View 或模板有关,有什么建议吗?

最佳答案

我想你需要查询

users = userComment.objects.filter(Q(Q(sender=request.user) & Q(recipient=username)) | Q(Q(sender=username) & Q(recipient=request.user))).order_by('sent_at')

关于python - 为什么我的私有(private)用户间聊天显示在其他用户的聊天个人资料中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41516187/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com