我创建了一个简单的消息传递应用程序,用户可以在其中相互发送消息。我选择创建此消息而不是使用插件,因为它可以让我自由地自定义代码
我创建了 2 个名为 message 和 read的函数,message 函数显示所有消息,并且它有一个与之关联的复选框以允许用户删除消息对象,并且 read 函数从显示所有消息的模板中捕获主键并获取对象并过滤该线程的所有对象。
我跟踪回复消息的方式是它们与特定的线程模型相关。并且因为如果 2 个用户不断回复彼此的消息,每条消息都是时间戳,我将通过仅过滤该会话的先前消息和最近消息来显示与该会话相关的所有消息。
这是一个棘手的问题,我感谢任何帮助我的人
我画了一张图来说明我的情况
此图片是消息功能,显示所有图片并允许用户删除自己的消息
这是引用称为读取的第二个函数的图表
http://s21.postimg.org/ribpiuwnb/image.jpg
class Thread(models.Model):
subject = models.CharField(max_length=100, blank=True)
user = models.ForeignKey(User)
class Message(models.Model):
user = models.ForeignKey(User, related_name='sender')
recipient = models.ForeignKey(User, related_name='recipient')
created = models.DateTimeField(auto_now_add=True)
body = models.CharField(max_length=1000)
read = models.BooleanField(default=False)
sentmessage = models.BooleanField(default=False)
thread = models.ForeignKey(Thread)
draft = models.BooleanField(default=False)
意见
@login_required
def message(request):
messages = Message.objects.filter(recipient=request.user).order_by("-created")
person = Person.objects.get(user=request.user)
if request.method =='POST':
delete_list = request.POST.get('hidden_field', False)
if delete_list:
values = [int(i) for i in delete_list.split("~")]
m = Message.objects.filter(pk__in=values)
m = m.filter(recipient=request.user )
m.delete()
return HttpResponseRedirect(reverse('world:message'))
return render(request,'messages.html',{'messages':messages,'person':person})
@login_required
def read(request,id):
try:
messages = Message.objects.get(pk=id,recipient=request.user)
except Message.DoesNotExist:
return HttpResponseRedirect(reverse('world:Display'))
messages.unread = True
messages.save()
if request.method =='POST':
form = ReplyForm(request.POST)
if form.is_valid():
id = request.POST.get('hidden_field', False)
try:
messages = Message.objects.get(pk=id ,recipient=request.user)
except Message.DoesNotExist or Thread.DOesNotExist:
return HttpResponseRedirect(reverse('world:LoginRequest'))
body = form.cleaned_data['body']
Message.objects.create(user=request.user,recipient=messages.user,body=body,thread=messages. thread,sentmessage=True,read=True)
return HttpResponseRedirect(reverse('world:message'))
message = Message.objects.filter(thread=messages.thread ).filter(created__lte=messages.created)
person = Person.objects.get(user=request.user)
initial = {}
initial.update({'hidden_field': messages.id})
form = ReplyForm(initial=initial)
return render(request,'read.html',{'messages':messages,'form':form,'message':message,'person':person})
消息.html
<form method="POST" >
{% csrf_token %}
{% for m in messages %}
<a class="theLink" href="{% url "world:read" m.id %}" >
<div class="username">{{m.user.username}}</div></a>
<div class="description">{{m.body}}</div><div id="container"> </div>
<input type="checkbox" value="{{m.id}}" class="messages id_check" >
<input type="hidden" value="" id="hidden_field" name="hidden_field" />
<input type = "button" id="deleteBttn" value= "Delete" class="box21"/>
{% endfor %}
</form>
</div>
读取.html
{% for m in message %}
<div class="wrapper">
<div class="user">{{m.user.username}} </div>
<div class="message">{{m.body}}</div>
</div>
{% endfor %}
<form method="POST" >{% csrf_token %}
{{form.body}}{{form.hidden_field}}
<input type = "submit" value= "send" class="sen"/>
</form>
<input type = "submit" value= "save" class="save"/>
如果消息被回复和转发并且我的消息功能显示所有未读消息并且还允许用户删除他自己的消息。如果他删除了一个与正在进行的对话相关的对象,它会影响两个用户,因为另一个用户将无法阅读假定与对话相关的已删除消息。我该如何解决这个问题?
谢谢