1
.. _ref-contrib-comments-example:
3
.. highlightlang:: html+django
5
===========================================
6
Example of using the in-built comments app
7
===========================================
9
Follow the first three steps of the quick start guide in the
10
:ref:`documentation <ref-contrib-comments-index>`.
12
Now suppose, you have an app (``blog``) with a model (``Post``)
13
to which you want to attach comments. Let us also suppose that
14
you have a template called ``blog_detail.html`` where you want
15
to display the comments list and comment form.
20
First, we should load the ``comment`` template tags in the
21
``blog_detail.html`` so that we can use it's functionality. So
22
just like all other custom template tag libraries::
26
Next, let us add the number of comments attached to the particular
27
model instance of ``Post``. For this we assume that a context
28
variable ``object_pk`` is present which gives the ``id`` of the
31
The usage of the :ttag:`get_comment_count` tag is like below::
33
{% get_comment_count for blog.post object_pk as comment_count %}
34
<p>{{ comment_count }} comments have been posted.</p>
36
If you have the instance (say ``entry``) of the model (``Post``)
37
available in the context, then you can refer to it directly::
39
{% get_comment_count for entry as comment_count %}
40
<p>{{ comment_count }} comments have been posted.</p>
44
Next, we can use the :ttag:`render_comment_list` tag, to render all comments
45
to the given instance (``entry``) by using the ``comments/list.html`` template.
47
{% render_comment_list for entry %}
49
Django will will look for the ``list.html`` under the following directories
52
comments/blog/post/list.html
53
comments/blog/list.html
56
To get a list of comments, we make use of the :ttag:`get_comment_list` tag.
57
This tag's usage is very similar to the :ttag:`get_comment_count` tag. We
58
need to remember that the :ttag:`get_comment_list` returns a list of comments
59
and hence we will have to iterate through them to display them::
61
{% get_comment_list for blog.post object_pk as comment_list %}
62
{% for comment in comment_list %}
63
<p>Posted by: {{ comment.user_name }} on {{ comment.submit_date }}</p>
65
<p>Comment: {{ comment.comment }}</p>
69
Finally, we display the comment form, enabling users to enter their
70
comments. There are two ways of doing so. The first is when you want to
71
display the comments template available under your ``comments/form.html``.
72
The other method gives you a chance to customize the form.
74
The first method makes use of the :ttag:`render_comment_form` tag. It's usage
75
too is similar to the other three tags we have discussed above::
77
{% render_comment_form for entry %}
79
It looks for the ``form.html`` under the following directories
82
comments/blog/post/form.html
83
comments/blog/form.html
86
Since we customize the form in the second method, we make use of another
87
tag called :ttag:`comment_form_target`. This tag on rendering gives the URL
88
where the comment form is posted. Without any :ref:`customization
89
<ref-contrib-comments-custom>`, :ttag:`comment_form_target` evaluates to
90
``/comments/post/``. We use this tag in the form's ``action`` attribute.
92
The :ttag:`get_comment_form` tag renders a ``form`` for a model instance by
93
creating a context variable. One can iterate over the ``form`` object to
94
get individual fields. This gives you fine-grain control over the form::
96
{% for field in form %}
97
{% ifequal field.name "comment" %}
98
<!-- Customize the "comment" field, say, make CSS changes -->
102
But let's look at a simple example::
104
{% get_comment_form for entry as form %}
105
<!-- A context variable called form is created with the necessary hidden
106
fields, timestamps and security hashes -->
108
<form action="{% comment_form_target %}" method="post">
112
<td><input type="submit" name="preview" class="submit-post" value="Preview"></td>
120
If you want your users to be able to flag comments (say for profanity), you
121
can just direct them (by placing a link in your comment list) to ``/flag/{{
122
comment.id }}/``. Similarly, a user with requisite permissions (``"Can
123
moderate comments"``) can approve and delete comments. This can also be
124
done through the ``admin`` as you'll see later. You might also want to
125
customize the following templates:
134
found under the directory structure we saw for ``form.html``.
139
Suppose you want to export a :ref:`feed <ref-contrib-syndication>` of the
140
latest comments, you can use the in-built :class:`LatestCommentFeed`. Just
141
enable it in your project's ``urls.py``:
143
.. code-block:: python
145
from django.conf.urls.defaults import *
146
from django.contrib.comments.feeds import LatestCommentFeed
149
'latest': LatestCommentFeed,
152
urlpatterns = patterns('',
154
(r'^feeds/(?P<url>.*)/$', 'django.contrib.syndication.views.feed',
155
{'feed_dict': feeds}),
159
Now you should have the latest comment feeds being served off ``/feeds/latest/``.
164
Now that we have the comments framework working, we might want to have some
165
moderation setup to administer the comments. The comments framework comes
166
in-built with :ref:`generic comment moderation
167
<ref-contrib-comments-moderation>`. The comment moderation has the following
168
features (all of which or only certain can be enabled):
170
* Enable comments for a particular model instance.
171
* Close comments after a particular (user-defined) number of days.
172
* Email new comments to the site-staff.
174
To enable comment moderation, we subclass the :class:`CommentModerator` and
175
register it with the moderation features we want. Let us suppose we want to
176
close comments after 7 days of posting and also send out an email to the
177
site staff. In ``blog/models.py``, we register a comment moderator in the
180
.. code-block:: python
182
from django.contrib.comments.moderation import CommentModerator, moderator
183
from django.db import models
185
class Post(models.Model):
186
title = models.CharField(max_length = 255)
187
content = models.TextField()
188
posted_date = models.DateTimeField()
190
class PostModerator(CommentModerator):
191
email_notification = True
192
auto_close_field = 'posted_date'
193
# Close the comments after 7 days.
196
moderator.register(Post, PostModerator)
198
The generic comment moderation also has the facility to remove comments.
199
These comments can then be moderated by any user who has access to the
200
``admin`` site and the ``Can moderate comments`` permission (can be set
201
under the ``Users`` page in the ``admin``).
203
The moderator can ``Flag``, ``Approve`` or ``Remove`` comments using the
204
``Action`` drop-down in the ``admin`` under the ``Comments`` page.
208
Only a super-user will be able to delete comments from the database.
209
``Remove Comments`` only sets the ``is_public`` attribute to