thevengefulco
thevengefulco

Reputation: 309

Pointing Django comments to specific objects

I am having trouble with pointing specific comments to specific objects in my Django website. On the HTML page below, 5 posts along with their respective attributes are displayed. Each post has it's own comment_form where users can leave comments. Yet, when a comment is left, ALL of the posts share the same comments, instead of their own unique set of comments. Does any one have a recommended way for me to give each post their own distinct set of comments so that they all aren't sharing the same ones?

Here is my current HTML:

    <div class="mainContent">
    <div class="content">
    {% for post in posts %}
        <article class="topContent">
            <div>
                <h1>{{ post.title }}</h1>
                <p>{{ post.body }}</p>
                <h6><i>published  {{ post.pub_date }}</i></h6>
                    <div class="commentForm">
                        {% for comment in comments %}
                        <p id="commentP">{{ comment.comment }} - {{ comment.name }}</p>
                        {% endfor %}
                    </div>
                <form method="POST" action="">
                {% csrf_token %}
                <div id='comment_form'>
                    <div id="namePrompt">
                        <p> Name:</p>
                    </div>
                    <div id="formName"> 
                        {{form.name}}
                    </div>
                    <div id="commentPrompt">
                        <p> Comment: </p>
                    <div id="formComment">
                        {{form.comment}} 
                    </div>
                    <div id="submitBtn">
                        <input type='submit' name='submit' value='Add Comment'>
                    </div>
                </div>
            </form>
            </div>
        </article>
        {% endfor %}
    </div>
</div>

here is the view:

def projects(request):

    print request
    posts = ProjectsPost.objects.all()
    comment_from_db = ProjectComment.objects.all()
    form = ProjectCommentForm(request.POST or None)

    if form.is_valid():
        new_form = form.save(commit=False)
        name = form.cleaned_data['name']
        comment = form.cleaned_data['comment']
        context_comment = ProjectComment.objects.get_or_create(name=name, comment=comment)
        print context_comment

    template = "blog/projects.html"
    context = {"form":form}
    project_posts = {'posts':posts}

    return render(request, template, {'form':form, 'posts':posts, 'comments':comment_from_db})`

Upvotes: 1

Views: 157

Answers (2)

bad_keypoints
bad_keypoints

Reputation: 1400

I'm assuming that your models look like these:

class ProjectPosts(models.Model):
    .... 

class ProjectComments(models.Model):
    ....
    post = models.ForeignKey(ProjectPosts)

Then in your template, you need to do this:

    <div class="commentForm">
        {% for comment in post.projectcomments_set.all %}
        <p id="commentP">{{ comment.comment }} - {{ comment.name }}</p>
        {% endfor %}
    </div>

Yeah. You can iterate over your reverse FK relations like that in templates.

EDIT: I'm seeing now that you're taking all the comments in your view, which is not for a specific post too. You don't need to do that. Whenever you need to show comments for a specific post, do in your template what I showed above.

Upvotes: 1

Bhargav
Bhargav

Reputation: 918

If there isn't an association between a post and it's related comment then each time you query the db you are getting all the comments and passing it on to the template.

As you mentioned in your own comment, adding a foreignkey on the Comment model should work. So when you query the comments for a particular article then you should be able to query only the comments related to that article.

So in essence something like this:

class ProjectPosts(models.Model):
    .... 

class ProjectComments(models.Model):
    ....
    post = models.ForeignKey(ProjectPosts)

And when retrieving all the comments related to a post you can do something like this:

post_comment = PorjectPosts.projectcomments_set.filter(id=foo)

You can read up more on realationships in the Django ORM here.

Upvotes: 0

Related Questions