am getting an error when trying to submit an form for blog comments please anyone to help me and direct me where l went wrong.
The error is GameArticleComment matching query does not exist. even l tried get_object_or_404 but l still get the same error.
Even I a added some pictures showing the source of error, below.
below is my code:
views.py
def article_detail(request, uid):
data = get_object_or_404( GameArticle, slug=uid, status='p')
comments = GameArticleComment.objects.filter(article= get_object_or_404(GameArticle, slug=uid), parent=None)
replies=GameArticleComment.objects.filter(article=get_object_or_404(GameArticle, slug=uid)).exclude(parent=None)
replyDict={}
for reply in replies:
if reply.parent.sno not in replyDict.keys():
replyDict[reply.sno]=[reply]
else:
replyDict[reply.sno] .append(reply)
context={'data':data,'comments':comments, 'replyDict':replyDict }
return render(request, 'Blog/article_details.html', context)
def Article_comment(request):
url= request.META.get('HTTP_REFERER')
if request.method == 'POST':
comment=request.POST.get("comment")
user=request.user
articlesno=request.POST.get("articlesno")
article=GameArticle.objects.get(sno=articlesno)
parentsno=request.POST.get("parentsno")
if parentsno == " ":
comment=GameArticleComment(comment=comment, user=user, article=article)
messages.success(request, 'Your Comment Has been Published Successfully')
comment.save()
return redirect(url)
else:
parent=GameArticleComment.objects.get(sno=parentsno)
comment=GameArticleComment(comment=comment, user=user, article=article, parent=parent)
messages.success(request, 'Reply has been Added Successfully')
comment.save()
return redirect(url)
return redirect(url)
models.py
class GameArticleComment(models.Model):
sno=models.AutoField(primary_key=True)
comment=models.TextField()
user=models.ForeignKey(User, on_delete=models.CASCADE)
article=models.ForeignKey(GameArticle, on_delete=models.CASCADE)
parent=models.ForeignKey('self', on_delete=models.CASCADE, null=True)
timestamp=models.DateTimeField(default=now)
class Meta:
ordering=['-timestamp']
extras.py
from django import template
register=template.Library()
@register.filter(name='get_reply')
def get_reply(dict, key):
return dict.get(key)
article_detail.html
{% load extras %}
<!--======= COMMENT FORM ====--->
<form action="/articlecomment" method="post">
{% csrf_token %}
<h4 class="fst-italic">Add a comment.... <small class="text-primary">({{ comments.count }})</small></h4>
<textarea id="textarea" name="comment" ></textarea>
<input type="hidden" name="articlesno" value="{{data.sno}}" >
<button type="submit" class="btn btn-outline-success mt-3 "> <i class="bi bi-send"></i> Send</button>
</form>
<!---====== REPLY FORM ======--->
<form action="/articlecomment" method="post" style="padding: 0.5rem;">
{% csrf_token %}
<textarea name="comment" id="" class="border border-warning form-control rip3 text-light bg-dark " style="padding: 5px;"></textarea>
<input type="hidden" name="parentsno" value="{{ comment.sno }}" >
<center>
<input type="hidden" name="articlesno" value="{{data.sno}}" >
<button type="submit" class="btn btn-success mt-3 bi bi-send">Send</button>
</center>
</form>
<!--====== REPLY LOOPING =====------->
{% for reply in replyDict|get_reply:comment.sno %}
<div class="d-flex align-items-stretch comment-data col-md-4 mb-3">
<div class="comment-user">
<img src="{% static 'assets/img/team/team-1.jpg' %}" alt="" srcset="">
<h6><small class="username text-warning">{{reply.user.username}}</small><small class="date text-muted">{{reply.timestamp|naturaltime}}</small></h6>
<div class="comment-details mt-3 mb-3 text-muted">
<small>{{reply.comment}}</small>
</div>
</div>
</div>
{% endfor %}
<!--========== COMMENT LOOPING =====-->
{% for comment in comments %}
<div class="comet mb-2">
<img src="{% static 'assets/img/team/team-1.jpg' %}" alt="" srcset="">
<strong>{{c.user.username }}</strong>
<small class="text-align-right h6 text-muted">{{comment.timestamp| naturaltime }}</small>
<div class="content">
<small class="toast-body ">{{ comment.comment }}</small>
</div>
</div>
{% endfor %}
At first I was able to send the comment but when l added The reply section, everything became worse, l don't know why

