[Django]-Django threadedcomments – how do I reply to a comment?

6👍

Seems like django threadedcommetns are not very popular here 🙂

I’ve found a great library: django-mptt

Here is my tutorial that describes how to implement threaded comments in django using django-mptt: http://codeblogging.net/blogs/1/3/

3👍

There is a “id_parent” div in the form, change the value to the parent’s id.

with jQuery something like this:

$('#commentForm').find("#id_parent").attr("value", divid);

1👍

Your reply to original looks good.

<form method="POST" action="{% get_comment_url post %}">
    {% csrf_token %}
    <ul>
        {% get_threaded_comment_form as form %}
        {{ form.as_ul }}
        <li><input type="submit" value="Submit Comment" /></li>
    </ul>
</form>

Say you want to keep reply form for every comment and keep it as threaded.

<div class="bulk">
    {% get_threaded_comment_tree for post as tree %}
        {% for comment in tree %}
            <div style="margin-left:{{comment.depth}}em;">
                {{comment}}
                Reply to this comment
                <form action="{% get_comment_url post comment %}" method="POST">
                    <ul>
                        {% get_threaded_comment_form as form %}
                        {{ form.as_ul }}
                        <li><input type="submit" value="Submit Reply" /></li>
                    </ul>
                </form>
            </div>
        {% endfor %}
</div>

Here we render the comment tree and keep a form beneath each comment which allows reply for that particular comment. The post url for this form is set as {% get_comment_url post comment %}. This is the only change we made apart from all that you described in the question. All this template tag says is that do a post but also set a parent for the reply. And the parent for the reply is the {{comment}} for which you are replying.

So, if you want “Reply for original”, you use {% get_comment_url post %}.

And if you want to reply for a particular comment, you use {% get_comment_url post comment %}.

0👍

A word to the wise: if you’re new to Django (or coding) and if you’re building something simple for demo or learning purposes – don’t use threaded comments. It’s more work than it’s worth. Just build a simple a comments model yourself. With that said, here’s how I got the replies to work, very similar to @akshar’s response.

list.html:

{% load threadedcomments_tags %}
<div id="comments">
    {% for comment in comment_list|fill_tree|annotate_tree %}
        {% if comment.open %}
            <ul>
        {% else %}
            </li>
        {% endif %}
        <li id="c{{ comment.id }}">{# c## is used by the absolute URL of the Comment model, so keep that as it is. #}
            <dl class="comment">
                <dt>
                    {{ comment.submit_date }} - {{ comment.name }}, ID: {{ comment.id }} <i>To test parent:{{ comment.parent_id }}</i>
                </dt>
                <dd>
                    {{ comment.comment|linebreaks }}
                    {% render_comment_form for object with comment.id %}
                </dd>
            </dl>
            {% for close in comment.close %}</li></ul>{% endfor %}
    {% endfor %}
</div>
👤Dt23

Leave a comment