Django Contrib Comments

November 21, 2009

Django has build in support for adding comments to model objects.

models.py

Add the following field to the model you want to add comments to

enable_comments = models.BooleanField(default=True)

This gives you a way of turning off comments for particular models.

urls.py

Add the comments url to the global urls.py

(r'^comments/', include('django.contrib.comments.urls')),

blog_detail.html

Add support to the template for adding comments

{% load comments %}

<h2><a name="comments" href="#"></a>Comments</h2>
{% get_comment_list for object as comment_list %}
{% for comment in comment_list %}
<p class="date">On {{comment.submit_date|date:"F j,Y" }}, {{ comment.user_name}} said:</p>
<blockquote>
{{comment.comment|markdown:"safe"}}
</blockquote>
{% endfor %}
<div id="add_comment">
{% if object.enable_comments %}
<h2>Add comment</h2>
{% render_comment_form for object %}
{% else %}
<p>Comments for this entry are closed.</p>
{% endif %}
</div>

You can configure the look and feel of the comment form by overriding the template comments/form.html. You can find an example of this, along with all the other templates for the comment system, in django/contrib/comments/templates/comments.

comments/preview.html

In order to present a preview of the comment, or a form to correct errors, you'll need to override comments/preview.html

{% extends "blog/template.html" %}
{% load comments %}
{% load markup %}
{% load i18n %}

{% block title %}Preview Comment{% endblock %}

{% block content %}
  {% load comments %}
  <form action="{% comment_form_target %}" method="post">{% csrf_token %}
   {% if next %}<input type="hidden" name="next" value="{{ next }}" />{% endif %}
    {% if form.errors %}
    <h2>{% blocktrans count form.errors|length as counter %}Please correct the error below{% plural %}Please correct the errors below{% endblocktrans %}</h2>
    {% else %}
    <h1>{% trans "Preview your comment" %}</h1>
      <blockquote>{{ comment|linebreaks }}</blockquote>
      <p>
      {% trans "and" %} <input type="submit" name="submit" class="submit-post" value="{% trans "Post your comment" %}" id="submit" /> {% trans "or make changes" %}:
      </p>
    {% endif %}
<table> 
    {% for field in form %}
      {% if field.is_hidden %}
        <tr><td>{{ field }}</td></td></tr>
       {% else %}
        {% if field.errors %}<tr><td colspan="2">{{ field.errors }}</td></tr>{% endif %}
          <tr{% ifequal field.name "honeypot" %} style="display:none;"{% endifequal %}><td>{{ field.label_tag }}</td><td>{{ field }}</td></tr>
      {% endif %}
    {% endfor %}
</table>    
    <p class="submit">
    <input type="submit" name="submit" class="submit-post" value="{% trans "Post" %}" />
    <input type="submit" name="preview" class="submit-preview" value="{% trans "Preview" %}" />
    </p>
  </form>
{% endblock %}

comments/posted.html

The comments/posted.html template is used when the comment has been successfully saved to the database.

{% extends "blog/template.html" %}
{% load comments %}

{% block title %}Comment Posted{% endblock %}

{% block content %}
<h2 class="title">Comment Posted</h2>
<div class="post">
<div class="entry">
<p>Your comment was posted succesfully
{% if comment.is_public %}
 - you can see it by <a href="{{comment.content_object.get_absolute_url}}">returning to the post</a>.
{% else %}
 - it has been held for moderation and, if deemed appropriate, will be approved in due course.
{% endif %}
</p>
</div> 
</div>
{% endblock %}

Tags: django comments