簡體   English   中英

如何將評論鏈接到 Django 中的單個帖子?

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

我有一個問題想幫我解決。 我有一個包含評論的“帖子”,但是,當我評論“帖子”時,我在“帖子 1”中發表的評論出現在“帖子 2”和所有“帖子”中,我想將評論鏈接到單個帖子,我一直在尋找解決方案,但我一直無法讓它發揮作用。

編輯我添加了我的帖子/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()

模型.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)

視圖.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})

我的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>

我的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>

知道如何讓評論起作用嗎? 謝謝!

在評論模型中嘗試刪除 unique=False

改變

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

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

執行上述操作並從那里取出

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM