如何创建在Django中显示用户配置文件图片的注释框

2024-06-16 10:01:37 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图创建一个评论框,显示用户的评论和他们的个人资料图片。我试图在下面的代码中实现这一点

评论模型

class comments(models.Model):
    sno = models.AutoField(primary_key=True)
    user = models.ForeignKey(MyUser, null=True, on_delete=models.CASCADE)
    post = models.ForeignKey(article, null=True, on_delete=models.CASCADE)
    comment = models.TextField(max_length=255)
    user_dp = models.OneToOneField(profilepic, null=True, on_delete=models.CASCADE)
    timedate = models.DateTimeField(auto_now_add=True, null=True)

    def __str__(self):
        return self.comment + " by " + self.user.name

视图.py

def postcomment(request):
    if request.method == "POST":
        comment = request.POST.get("comment")
        user = request.user
        postSno = request.POST.get("postSno")
        post = article.objects.get(sno= postSno)
        user_dp =

        comment = comments(comment = comment, user = user,  post = post, user_dp=user_dp)
        comment.save()
        print("comment saved")
    return redirect(f"/readfull/{post.slug}")

html

{% for comnt in feedback %}
                <ul class="list-unstyled" style="margin: 5px;">
                  <li class="media" style="padding: 0px;">

                        <img src="{{media_url}}{{ comnt.user_dp.propic }}" class="card-img-top profilePic" alt="DP" style="width:50px; height:50px;"><br><br>
                      
                  <div class="media-body" style="padding: 0px;">
                    <p class="mt-0 mb-1 list-group-item list-group-item-info" id="commentname">{{comnt.user.name}}</p>
                    <p class="card-text list-group-item list-group-item-secondary" id="comment" >{{comnt.comment}} <button type="button" class="close" aria-label="Close">
  <span aria-hidden="true">&times;</span>
</button><br><span id="timedate">{{comnt.timedate}}</span></p>
                  </div>
                  </li>
                </ul>
{% endfor %}

Tags: truestylemodelsrequestgroupcommentitempost