首页 > 解决方案 > 当用户回复评论时,它会显示评论时间。它应该显示实际时间。我该如何解决?

问题描述

我正在学习django。我正在写博客。当用户回复评论时,它会显示其父评论的时间。它应该显示实际的回复时间。我在这篇文章中附上了一张图片。请看一下,你会更好地理解我的问题。我该如何解决?我试过了,但都是徒劳的。可能是我犯了一个愚蠢的错误,或者我没有得到它。提前致谢

视图.py

def blogPost(request, slug):
    post = Post.objects.filter(slug=slug).first()
    comments = BlogComment.objects.filter(post=post, parent=None)
    replies = BlogComment.objects.filter(post=post).exclude(parent=None)
    replyDict = {}
    for reply in replies:
        if reply.parent.sno not in replyDict.keys():
            replyDict[reply.parent.sno] = [reply]
        else:
            replyDict[reply.parent.sno].append(reply)

    context = {'post':post, 'comments':comments, 'user': request.user, 'replyDict': replyDict}
    return render(request, 'blog/blogPost.html',context)

def postComments(request):
    if request.method == 'POST':
        comment = request.POST.get('comment')
        user = request.user
        postSno = request.POST.get('postSno')
        post = Post.objects.get(sno=postSno)
        parentSno = request.POST.get('parentSno')
        if parentSno == "":
            comments = BlogComment(comment=comment, user=user, post=post)
            comments.save()
            messages.success(request, 'Your Comment has been posted Successfully')
        else:
            parent = BlogComment.objects.get(sno=parentSno)
            comments = BlogComment(comment=comment, user=user, post=post, parent=parent)
            comments.save()
            messages.success(request, 'Your reply has been posted Successfully')
                  
        
    return redirect(f"/blog/{post.slug}")

模型.py

class Post(models.Model):
    sno = models.AutoField(primary_key=True)
    title = models.CharField(max_length=200)
    content = models.TextField(max_length=10000)
    author = models.CharField(max_length=20)
    region = models.CharField(max_length=20)
    slug = models.CharField(max_length=50, default="")
    timestamp = models.DateTimeField(blank=True)
    thumbnail = models.ImageField(upload_to="images", default="")

    def __str__(self):
        return self.title + 'by' + self.author


class BlogComment(models.Model):
    sno = models.AutoField(primary_key=True)
    comment = models.TextField()
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    post = models.ForeignKey(Post, on_delete=models.CASCADE)
    parent = models.ForeignKey('self', on_delete=models.CASCADE, null=True) #it is pointing the value of blogcomment so we use 'self'
    timestamp = models.DateTimeField(default=now)
    replytimestamp = models.DateTimeField(default=now)

    

    def __str__(self):
        return self.comment[0:10] + "..." + "by " + self.user.username
    

blogPost.html

    {% for comment in comments %}
    <div class="row border border-dark mx-0 my-3">
        <div class="col-md-1"><img src="/media/images/usr.png" height="55px" width="55px"></div>

        <div class="col-md-11"><b> {{comment.user.username}} </b>
            <span class="badge badge-secondary">{{comment.timestamp | naturaltime}}</span>
            <!--this will show time in 2 hours ago or x hour ago like-->

            <div>
                <p class="font-italic">{{comment.comment}}</p>
            </div>
            <div class="reply mx-0">
                <p>
                    {% if user.is_authenticated %}
                    <button class="btn btn-primary btn-sm" type="button" data-toggle="collapse"
                        data-target="#replyBox{{comment.sno}}" aria-expanded="false" aria-controls="collapseExample">
                        reply
                    </button>
                </p>
                <div class="collapse" id="replyBox{{comment.sno}}">
                    <div class="card card-body mb-2">
                        <form action="/blog/postComments" method="POST">{% csrf_token %}
                            <div class="form-group">
                                <label for="comment">Post a Reply</label>
                                <input type="text" class="form-control" id="comment" name="comment"
                                    placeholder="Write a reply Here">
                                <input type="hidden" name="parentSno" value="{{comment.sno}}">
                            </div>
                            <input type="hidden" name="postSno" value="{{post.sno}}">
                            <button type="submit" class="btn btn-primary">Submit</button>
                        </form>
                    </div>
                </div>
                {% endif %}
                <div class="replies my-2">
                    {% for reply in replyDict|get_val:comment.sno %}
                    <!-- this will return replies associated with comment.sno in the replyDict[] -->
                    <div class="row my-2">
                        <div class="col-md-1"><img src="/media/images/usr.png" height="35px" width="35px"></div>

                        <div class="col-md-11">
                            <b> {{comment.user.username}} </b><span
                                class="badge badge-secondary">{{comment.timestamp | naturaltime}}</span>

                            <div>{{reply.comment}}</div>
                        </div>
                        <br>
                    </div>
                    {% endfor %}

                </div>
            </div>
        </div>
    </div>

    {% endfor %}
</div>
{% endblock %}

评论和回复图片

标签: djangodjango-modelsdjango-viewsdjango-templates

解决方案


推荐阅读