开发者

Django threadedcomments - how do I reply to a comment?

开发者 https://www.devze.com 2023-03-05 05:35 出处:网络
I am trying to integrate threadedcommetns to my Django app and having trouble in uderstanding how it works. Here is how my template looks (based on example from tutorial):

I am trying to integrate threadedcommetns to my Django app and having trouble in uderstanding how it works. Here is how my template looks (based on example from tutorial):

&l开发者_Go百科t;h3>Comments on This Post:</h3>
{% get_threaded_comment_tree for post as tree %}
{% for comment in tree %}
    <div style="margin-left: {{ comment.depth }}em;" class="comment">
        {% link_to_profile comment.user %}
        {% auto_transform_markup comment %}
    </div>
{% endfor %}
<p>Reply to Original:</p>
<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>

So, if those are threaded comments, how do I reply to a comment that is already left by someone? Where is the form for that? I only managed to get Reply to Original form, but with this, comments are not threaded at all.

I would be very grateful for your help.

P.S. Actually,I am not very happy how this app is working with django 1.3, so suggesting an alternative would be a great answer too.


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/


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);


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 %}.


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>
0

精彩评论

暂无评论...
验证码 换一张
取 消