Yet another threaded comments app

Sat 10 January 2009 by Thejaswi Puthraya

A reusable threaded comments app

This week I am releasing code for yet another reusable app and yet another threaded-comments app. There are quite a few threaded comment apps around but the best one being this by Eric Florenzano.

The motive of releasing this code is not to compete with any of the apps available but as a proof of how portable django and it's apps (here I am referring to the comments app, but the statement holds true for most third-party apps) can be.

Probably with some work this app should be as good as other apps.

Procedure to use it

  • Download the source from here and place the threaded_comments app in your django project.

  • Patch your django source with the latest patch from #8630.

  • Add the following attributes in the settings.py.

    COMMENTS_APP = "threaded_comments"
    
  • Make use of the new comment tags that are available to help with the threaded support. Here's an example template:

     {% extends "test_app/base.html" %}
    
     {% block head %}
     <script type="text/javascript">
       function showCommentForm(id) {
           var commentForm = document.getElementById('commentForm'+id);
           var linkHolder  = document.getElementById('linkReply'+id);
           if (commentForm.style.display == "none") {
               commentForm.style.display = "block";
               linkHolder.innerHTML="Hide Reply";
           } else {
               commentForm.style.display = "none";
               linkHolder.innerHTML="Reply";
           }
       }
    </script>
    {% endblock %}
    
    {% block content %}
    {% load threaded_comments %}
    {% get_comment_list for app.model object.pk as comment_list order_by thread %}
    {# To order comments by date use order_by date #}
    {% if comment_list %}
      <h2>Comments</h2>
      {% for comment in comment_list %}
      <div id="c{{ comment.id }}" style="margin-left: {{ comment.level }}0px;">
        <h4>{{ comment.name|escape }}
            {% if comment.parent %}
               in reply to
               <a href="#c{{ comment.parent_id }}">{{ comment.parent_id }}</a>
            {% endif %}
               at {{ comment.submit_date|date:"r"}}</h4>
        <p>{{ comment.comment|escape|urlizetrunc:"100"|linebreaks }}</p>
        <p><a id="linkReply{{ comment.id }}" href="#"
              onclick="showCommentForm({{ comment.id }});">Reply</a></p>
        <div id="commentForm{{ comment.id }}" style="display: none">
           <h4>Reply to the above comment ({{ comment.id }})</h4>
           {% render_comment_form for app.model object.pk with comment.id %}
           {# Alternatively use #}
           {# {% get_comment_form for app.model object.pk as form with parent_id %} #}
        </div>
      </div>
      <hr>
      {% endfor %}
    {% endif %}
    <h2>Leave a comment</h2>
    {% render_comment_form for app.model object.pk %}
    {% endblock %}
    
  • Most of the code is unchanged except for the comment tags which now take additional arguments and some optional javascript.

As mentioned in my previous post, the toughest decision is to patch Django because people don't like to pollute their code bases (don't even ask about organizations, they get hysterical when you want to patch any code base).

Please let me know how you find this app and suggest ways to make it DRYer and better.