example.txt 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. .. highlightlang:: html+django
  2. ===========================================
  3. Example of using the built-in comments app
  4. ===========================================
  5. Follow the first three steps of the quick start guide in the
  6. :doc:`documentation </ref/contrib/comments/index>`.
  7. Now suppose, you have an app (``blog``) with a model (``Post``)
  8. to which you want to attach comments. Let's also suppose that
  9. you have a template called ``blog_detail.html`` where you want
  10. to display the comments list and comment form.
  11. Template
  12. ========
  13. First, we should load the ``comment`` template tags in the
  14. ``blog_detail.html`` so that we can use its functionality. So
  15. just like all other custom template tag libraries::
  16. {% load comments %}
  17. Next, let's add the number of comments attached to the particular
  18. model instance of ``Post``. For this we assume that a context
  19. variable ``object_pk`` is present which gives the ``id`` of the
  20. instance of ``Post``.
  21. The usage of the :ttag:`get_comment_count` tag is like below::
  22. {% get_comment_count for blog.post object_pk as comment_count %}
  23. <p>{{ comment_count }} comments have been posted.</p>
  24. If you have the instance (say ``entry``) of the model (``Post``)
  25. available in the context, then you can refer to it directly::
  26. {% get_comment_count for entry as comment_count %}
  27. <p>{{ comment_count }} comments have been posted.</p>
  28. Next, we can use the :ttag:`render_comment_list` tag, to render all comments
  29. to the given instance (``entry``) by using the ``comments/list.html`` template::
  30. {% render_comment_list for entry %}
  31. Django will will look for the ``list.html`` under the following directories
  32. (for our example)::
  33. comments/blog/post/list.html
  34. comments/blog/list.html
  35. comments/list.html
  36. To get a list of comments, we make use of the :ttag:`get_comment_list` tag.
  37. Using this tag is very similar to the :ttag:`get_comment_count` tag. We
  38. need to remember that :ttag:`get_comment_list` returns a list of comments
  39. and hence we have to iterate through them to display them::
  40. {% get_comment_list for blog.post object_pk as comment_list %}
  41. {% for comment in comment_list %}
  42. <p>Posted by: {{ comment.user_name }} on {{ comment.submit_date }}</p>
  43. ...
  44. <p>Comment: {{ comment.comment }}</p>
  45. ...
  46. {% endfor %}
  47. Finally, we display the comment form, enabling users to enter their
  48. comments. There are two ways of doing so. The first is when you want to
  49. display the comments template available under your ``comments/form.html``.
  50. The other method gives you a chance to customize the form.
  51. The first method makes use of the :ttag:`render_comment_form` tag. Its usage
  52. too is similar to the other three tags we have discussed above::
  53. {% render_comment_form for entry %}
  54. It looks for the ``form.html`` under the following directories
  55. (for our example)::
  56. comments/blog/post/form.html
  57. comments/blog/form.html
  58. comments/form.html
  59. Since we customize the form in the second method, we make use of another
  60. tag called :ttag:`comment_form_target`. This tag on rendering gives the URL
  61. where the comment form is posted. Without any :doc:`customization
  62. </ref/contrib/comments/custom>`, :ttag:`comment_form_target` evaluates to
  63. ``/comments/post/``. We use this tag in the form's ``action`` attribute.
  64. The :ttag:`get_comment_form` tag renders a ``form`` for a model instance by
  65. creating a context variable. One can iterate over the ``form`` object to
  66. get individual fields. This gives you fine-grain control over the form::
  67. {% for field in form %}
  68. {% ifequal field.name "comment" %}
  69. <!-- Customize the "comment" field, say, make CSS changes -->
  70. ...
  71. {% endfor %}
  72. But let's look at a simple example::
  73. {% get_comment_form for entry as form %}
  74. <!-- A context variable called form is created with the necessary hidden
  75. fields, timestamps and security hashes -->
  76. <table>
  77. <form action="{% comment_form_target %}" method="post">
  78. {% csrf_token %}
  79. {{ form }}
  80. <tr>
  81. <td colspan="2">
  82. <input type="submit" name="submit" value="Post">
  83. <input type="submit" name="preview" value="Preview">
  84. </td>
  85. </tr>
  86. </form>
  87. </table>
  88. Flagging
  89. ========
  90. If you want your users to be able to flag comments (say for profanity), you
  91. can just direct them (by placing a link in your comment list) to ``/flag/{{
  92. comment.id }}/``. Similarly, a user with requisite permissions (``"Can
  93. moderate comments"``) can approve and delete comments. This can also be
  94. done through the ``admin`` as you'll see later. You might also want to
  95. customize the following templates:
  96. * ``flag.html``
  97. * ``flagged.html``
  98. * ``approve.html``
  99. * ``approved.html``
  100. * ``delete.html``
  101. * ``deleted.html``
  102. found under the directory structure we saw for ``form.html``.
  103. Feeds
  104. =====
  105. Suppose you want to export a :doc:`feed </ref/contrib/syndication>` of the
  106. latest comments, you can use the built-in :class:`LatestCommentFeed`. Just
  107. enable it in your project's ``urls.py``:
  108. .. code-block:: python
  109. from django.conf.urls import patterns, url, include
  110. from django.contrib.comments.feeds import LatestCommentFeed
  111. urlpatterns = patterns('',
  112. # ...
  113. (r'^feeds/latest/$', LatestCommentFeed()),
  114. # ...
  115. )
  116. Now you should have the latest comment feeds being served off ``/feeds/latest/``.
  117. Moderation
  118. ==========
  119. Now that we have the comments framework working, we might want to have some
  120. moderation setup to administer the comments. The comments framework comes
  121. built-in with :doc:`generic comment moderation
  122. </ref/contrib/comments/moderation>`. The comment moderation has the following
  123. features (all of which or only certain can be enabled):
  124. * Enable comments for a particular model instance.
  125. * Close comments after a particular (user-defined) number of days.
  126. * Email new comments to the site-staff.
  127. To enable comment moderation, we subclass the :class:`CommentModerator` and
  128. register it with the moderation features we want. Let's suppose we want to
  129. close comments after 7 days of posting and also send out an email to the
  130. site staff. In ``blog/models.py``, we register a comment moderator in the
  131. following way:
  132. .. code-block:: python
  133. from django.contrib.comments.moderation import CommentModerator, moderator
  134. from django.db import models
  135. class Post(models.Model):
  136. title = models.CharField(max_length = 255)
  137. content = models.TextField()
  138. posted_date = models.DateTimeField()
  139. class PostModerator(CommentModerator):
  140. email_notification = True
  141. auto_close_field = 'posted_date'
  142. # Close the comments after 7 days.
  143. close_after = 7
  144. moderator.register(Post, PostModerator)
  145. The generic comment moderation also has the facility to remove comments.
  146. These comments can then be moderated by any user who has access to the
  147. ``admin`` site and the ``Can moderate comments`` permission (can be set
  148. under the ``Users`` page in the ``admin``).
  149. The moderator can ``Flag``, ``Approve`` or ``Remove`` comments using the
  150. ``Action`` drop-down in the ``admin`` under the ``Comments`` page.
  151. .. note::
  152. Only a super-user will be able to delete comments from the database.
  153. ``Remove Comments`` only sets the ``is_public`` attribute to
  154. ``False``.