example.txt 7.4 KB

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