Am trying to create a conversation between the user and the owner of the item. Everything goes well but the challenge is that, when the user send message to the owner of the item once, he cannot sent message again and will be redirected to the unexisting item id, thereby raising an doesnotexist error. I wander what the problem is.
this is the model
class Items(models.Model):
user = models.ForeignKey(User, on_delete = models.CASCADE, related_name='supplier')
store = models.ManyToManyField(Store)
title = models.CharField(max_length=10, null=True, blank =True)
description = models.CharField(max_length=1000, null=True, blank=True)
price = models.CharField(max_length=20, null=True, blank=True)
phone = models.CharField(max_length=20, null=True, blank=True)
location =models.CharField(max_length=20, null=True, blank=True)
post_date = models.DateTimeField(auto_now_add=True, null=True, blank=True)
thumbnails =models.ImageField(upload_to='thumbnails/', null=True, blank=True)
def __str__(self):
return self.title
class Meta:
ordering =['-post_date',]
verbose_name_plural ='Items'
class Images(models.Model):
image =models.ImageField(upload_to='media/', null=True, blank=True)
item=models.ForeignKey(Items, on_delete = models.CASCADE, null=True, blank = True)
def __str__(self):
return str(self.item.title)
class Meta:
verbose_name_plural ='Images'
class Conversation(models.Model):
item = models.ForeignKey(Items, related_name='conversations', on_delete=models.CASCADE)
members = models.ManyToManyField(User, related_name='conversations')
created_at = models.DateTimeField(auto_now_add=True)
modified_at = models.DateTimeField(auto_now=True)
class Meta:
ordering = ('-modified_at',)
class ConversationMessage(models.Model):
conversation = models.ForeignKey(Conversation, related_name='messages', on_delete=models.CASCADE)
content = models.TextField()
created_at = models.DateTimeField(auto_now_add=True)
created_by = models.ForeignKey(User, related_name='created_messages', on_delete=models.CASCADE)
this is the views
def detail(request, pk):
user = request.user
item =Items.objects.all()
item_url = reverse('chat', args=[pk])
try:
items = Items.objects.get(pk = pk)
except Items.DoesNotExist:
items = Items.objects.filter(pk = pk)
counts = Images.objects.aggregate(count = Count('image'))
context = {
'detail_item':items,
'all_item':item,
'user':user,
'count':counts,
'item_url':item_url
}
return render(request, 'jijiapp/jijidetail.html', context)
this is the chat form
def chat(request, item_id):
item = get_object_or_404(Items, pk=item_id)
if item.user == request.user:
return redirect('index')
conversations = Conversation.objects.filter(item=item).filter(members__in=[request.user.id])
if conversations:
return redirect('detail', pk=conversations.first().id)
if request.method =='POST':
# the new conversationn
form = ConversationMessageForm(request.POST)
if form.is_valid():
conversation = Conversation.objects.create(item=item)
conversation.members.add(request.user)
conversation.members.add(item.user)
conversation.save()
conversation_message = form.save(commit=False)
conversation_message.conversation = conversation
conversation_message.created_by = request.user
conversation_message.save()
return redirect('chat', pk=item_id)
# reverse('chat', args=[item_id])
form = ConversationMessageForm()
context = {
'form':form,
'item':item,
'conversations':conversations,
}
return render(request, 'jijiapp/chatmessage.html', context)
this is the route from detail page
<a href="{{ item_url }}" class=" btn btn-secondary num" style="font-size: 1.5rem;"> Chat with {{ item.user }}! You can negotiate the price</a>