简体   繁体   English

为每篇文章Django创建单独的评论部分

[英]Creating separate comment section per article Django

I am trying to create a comments section that is separate from each topic. 我正在尝试创建一个与每个主题分开的评论部分。 for some reason the comments app that I created will show all comments on every topic. 由于某种原因,我创建的评论应用程序将显示每个主题的所有评论。 for example if I were to make a comment on topic 1 that same comment would appear on topic 2. 例如,如果我要对主题1进行评论,则相同的评论将出现在主题2上。

topic 1: 主题1:

Comment: blah 评论:等等

topic 2: 主题2:

Comment: blah 评论:等等

comments app: models.py 评论应用:models.py

from django.db import models
from django.conf import settings

from blogging_logs.models import Topic
# Create your models here.


class Comment(models.Model):
    user = models.ForeignKey(settings.AUTH_USER_MODEL, default=1, on_delete=models.CASCADE)
    topic = models.ForeignKey(Topic, on_delete=models.CASCADE)
    content = models.TextField()
    date_added = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return str(self.content)

forms.py (In blogging_logs app) Forms.py(在blogging_logs应用中)

from django import forms
from .models import Category, Topic, Entry
from comments.models import Comment

class CommentForm(forms.ModelForm):
    class Meta:
        model = Comment
        fields = ['content']
        labels = {'text': ''}
        widgets = {'text': forms.Textarea(attrs={'cols': 80})}

view.py (In blogging_logs app) view.py(在blogging_logs应用中)

from comments.models import Comment
from .models import Category, Entry, Topic
from .forms import CategoryForm, TopicForm, EntryForm, CommentForm


def topic(request, entry_id):
    """Show entry for single topic"""
    topic = Topic.objects.get(id=entry_id)
    entries = topic.entry_set.all()
    comments = Comment.objects.all()

    if request.method != 'POST':
        # No comment submitted
        form = CommentForm()
    else:
        # Comment posted
        form = CommentForm(data=request.POST)
        if form.is_valid():
            new_comment = form.save(commit=False)
            new_comment.topic = topic
            new_comment.user = request.user
            new_comment.save()
            return HttpResponseRedirect(reverse('blogging_logs:topic', args=[entry_id]))

    context = {'topic': topic, 'entries': entries, 'comments': comments, 'form': form}
    return render(request, 'blogging_logs/topic.html', context)

topic.html topic.html

<h3> Comments Section </h3>
<form class="" action="{% url 'blogging_logs:topic' topic.id%}" method="post">
  {% csrf_token %}
  {{ form.as_p }}
  <button name='submit'> Add Comment </button>
</form>
  <div>
    {% for comment in comments %}
      {{ comment }}
      <p>{{comment.date_added|date:'M d, Y H:i' }}</p>
      <p>{{comment.user }}</p>
      <p>
        <a href="{% url 'blogging_logs:delete_comment' comment.id %}">Delete comment</a>
      </p>
    {% empty %}
      <p>no comments entered yet.</p>
    {% endfor %}
  </div>

I thought by grabbing the entry_id associated with a topic it would save to that specific topic but it doesn't. 我想通过抓住与某个主题相关联的entry_id可以将其保存到该特定主题,但事实并非如此。 Any help would be appreciated. 任何帮助,将不胜感激。

You probably want 你可能想要

def topic(request, entry_id):
    """Show entry for single topic"""
    topic = Topic.objects.get(id=entry_id)
    entries = topic.entry_set.all()
    # This selects all comments, not what you want
    # comments = Comment.objects.all()
    # Instead you should select just the topic's comments
    comments = topic.comment_set.all()

Note this uses the reverse foreign key relationship from topic . 注意,这使用与topic反向的外键关系。

https://docs.djangoproject.com/en/2.1/topics/db/queries/#related-objects https://docs.djangoproject.com/en/2.1/topics/db/queries/#related-objects

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

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