Comments
Published on May 19, 2007Django really is a wonderful piece of software. After watching a video of Jacob Kaplan-Moss' presentation at google where he mentioned that users can comment on almost all content on LJWorld.com I thought "hey that's a pretty cool idea, I want to do that too". So I did, but the really cool thing is just how easy it was to do.
First add 'django.contrib.comments', to your INSTALLED_APPS in settings.py
If your using a custom view instead of django's generic views then you'll need to add
from django.contrib.comments.models import FreeComment
to your views.py
At the top of the template where you want to display the comments add this in
{% load comments.comments %}
Now the slightly tricky part. In your template you want to add this line
{% get_free_comment_list for APP.MODELNAME object.id as comment_list %}
where APP is the name of the app in question, this must be all lowercase & MODELNAME is the name of the model which the comments are related to, again all lowercase
Then to actually display the comments add this code
{% for comment in comment_list %}
<p><b>{{ comment.person_name|escape }}</b> commented, on
{{ comment.submit_date|date:"F j, Y" }}
at {{ comment.submit_date|date:"P" }}:</p>
{{ comment.comment|escape|urlizetrunc:40|linebreaks }}
{% endfor %}
To display a form where people can post a comment you need three files
templates/comments/freeform.html
{% if display_form %}
<form action="/comments/postfree/" method="post">
<p>Your name: <input type="text" id="id_person_name" name="person_name" /></p>
<p>Comment:<br />
<textarea name="comment" id="id_comment" rows="10" cols="60"></textarea></p>
<input type="hidden" name="options" value="{{ options }}" />
<input type="hidden" name="target" value="{{ target }}" />
<input type="hidden" name="gonzo" value="{{ hash }}" />
<p><input type="submit" name="preview" value="Preview comment" /></p>
</form>
{% endif %}
templates/comments/free_preview.html
<h1>Preview your comment</h1>
<form action="../postfree/" method="post">
{% if comment_form.has_errors %}
<p><strong style="color: red;">Please correct the following errors.</strong></p>
{% else %}
<div class="comment">
{{ comment.comment|escape|urlizetrunc:"40"|linebreaks }}
<p class="date small">Posted by <strong>{{ comment.person_name|escape }}</strong></p>
</div>
<p><input type="submit" name="post" value="Post public comment" /></p>
<h1>Or edit it again</h1>
{% endif %}
{% if comment_form.person_name.errors %}
{{ comment_form.person_name.html_error_list }}
{% endif %}
<p><label for="id_person_name">Your name:</label> {{ comment_form.person_name|escape }}</p>
{% if comment_form.comment.errors %}
{{ comment_form.comment.html_error_list }}
{% endif %}
<p>
<label for="id_comment">Comment:</label>
<br />
{{ comment_form.comment }}
</p>
<input type="hidden" name="options" value="{{ options }}" />
<input type="hidden" name="target" value="{{ target }}" />
<input type="hidden" name="gonzo" value="{{ hash }}" />
<p>
<input type="submit" name="preview" value="Preview revised comment" />
</p>
</form>
and templates/comments/posted.html
<h1>Comment posted successfully</h1>
<p>Thanks for contributing.</p>
{% if object %}
<ul>
<li><a href="{{ object.get_absolute_url }}">View your comment</a></li>
</ul>
{% endif %}
{% free_comment_form for APP.MODELNAME object.id %}
Then it's just a matter of adding
{% free_comment_form for blog.entry object.id %}
to your template wherever you want the form to appear.
One other handy thing I should mention is comment counts. This line will get you the number of comments for the object in question and store it in the comment_count variable
{% get_free_comment_count for APP.MODELNAME object.id as comment_count %}
You can then use this variable in your template to display the number of comments for, say a particular blog entry.
<p>{{ comment_count }} comment{{ comment_count|pluralize }}</p>



February 23, 2008 at 4:23 p.m.
I fully agree Django is super