简体   繁体   English

如何将评论链接到 Django 中的单个帖子?

[英]How to link a comment to a single post in django?

I have a problem which I want to help me solve.我有一个问题想帮我解决。 I have a "Post" with comments included but, when I comment a "Post" the comment I made in "Post 1" appears in "Post 2" and in all "Posts" and I want to link the comment to a single post, I've been looking for solutions but I have not been able to make it work.我有一个包含评论的“帖子”,但是,当我评论“帖子”时,我在“帖子 1”中发表的评论出现在“帖子 2”和所有“帖子”中,我想将评论链接到单个帖子,我一直在寻找解决方案,但我一直无法让它发挥作用。

EDIT I ADDED MY post/models.py 编辑我添加了我的帖子/models.py
 class Post(models.Model): owner = models.ForeignKey(User, on_delete=models.CASCADE) title = models.CharField(max_length=200) slug = models.SlugField(unique=True) autor = models.CharField(max_length=200) description = models.TextField() likes = models.PositiveIntegerField(default=0) created_date = models.DateTimeField(default=timezone.now) published_date = models.DateTimeField(blank=True, null=True) files = models.FileField(upload_to=upload_location, validators= [validate_file_extension]) post_type = models.CharField(max_length=100, choices=Book_Type_Choices) tags = TaggableManager()

models.py模型.py

 class Comment(models.Model): post = models.ForeignKey(Post, related_name='cooments') user = models.ForeignKey(User, unique=False) text = models.CharField(max_length=250) created_date = models.DateTimeField(default=timezone.now) approved_comment = models.BooleanField(default=False)

views.py视图.py

 @login_required def add_comment_to_post(request, pk): post= get_object_or_404(Post, pk=pk) if request.method == 'POST': form = CommentForm(request.POST) if form.is_valid(): comment = form.save(commit=False) comment.post= post comment.user = request.user comment.save() return redirect('post_detail', slug=post.slug) else: form = CommentForm() return render(request, 'comments/add_comment_to_post.html', {'form':form})

my add_comment.html , this code fragment is included on my post_detail.html我的add_comment.html ,此代码片段包含在我的post_detail.html 中

<form action="{% url 'add_comment_to_post' pk=post.pk %}" method="post"> {% csrf_token %} {{form.as_p}} <input type="submit" class="btn btn-primary" value="Comentar"> </form>

my book_detail.html我的book_detail.html

 <div class="container"> <div class="row"> <div class="col-md-6 comment" style="text-align: center;"> <!-- HERE IS WHERE I INCLUDE THE add_comment.html --> {% include 'comments/add_comment_to_post.html' %} </div> {% for comment in comments %} {% if user.is_authenticated or comment.approved_comment %} <div class="col-sm-6 col-sm-offset-1"> <div class="media-body"> <div class="well well-lg"> <div class="avatar"> <img src="{{comment.user.profile.pic.thumbnail.url}}" class="img-responsive img-circle" alt=""> </div> <h4 class="media-heading text-uppercase reviews">{{comment.user.get_full_name}} </h4> <ul class="media-date text-uppercase reviews list-inline"> <li>{{comment.created_date}}</li> </ul> <p class="media-comment"> {{comment.text}} </p> <a class="btn btn-info btn-circle text-uppercase" href="#" id="reply"><span class="glyphicon glyphicon-share-alt"></span> Reply</a> <a class="btn btn-warning btn-circle text-uppercase" data-toggle="collapse" href="#replyOne"><span class="glyphicon glyphicon-comment"></span> 2 comments</a> </div> </div> </div> {% endif %} {% empty %} <p>No hay comentarios que mostrar :(</p> {% endfor %} </div> </div>

Any idea how I can make comments work?知道如何让评论起作用吗? Thanks!谢谢!

In the comment model try to remove the unique=False在评论模型中尝试删除 unique=False

Change改变

class Comment(models.Model):
   user = models.ForeignKey(User, unique=False)

to

class Comment(models.Model):
    user = models.ForeignKey(User) # remove unique=False

Do the above and take it from there执行上述操作并从那里取出

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM