index.txt 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. ===========================
  2. Django's comments framework
  3. ===========================
  4. .. module:: django.contrib.comments
  5. :synopsis: Django's comment framework
  6. .. highlightlang:: html+django
  7. Django includes a simple, yet customizable comments framework. The built-in
  8. comments framework can be used to attach comments to any model, so you can use
  9. it for comments on blog entries, photos, book chapters, or anything else.
  10. .. note::
  11. If you used to use Django's older (undocumented) comments framework, you'll
  12. need to upgrade. See the :doc:`upgrade guide </ref/contrib/comments/upgrade>`
  13. for instructions.
  14. Quick start guide
  15. =================
  16. To get started using the ``comments`` app, follow these steps:
  17. #. Install the comments framework by adding ``'django.contrib.comments'`` to
  18. :setting:`INSTALLED_APPS`.
  19. #. Run ``manage.py syncdb`` so that Django will create the comment tables.
  20. #. Add the comment app's URLs to your project's ``urls.py``:
  21. .. code-block:: python
  22. urlpatterns = patterns('',
  23. ...
  24. (r'^comments/', include('django.contrib.comments.urls')),
  25. ...
  26. )
  27. #. Use the `comment template tags`_ below to embed comments in your
  28. templates.
  29. You might also want to examine :doc:`/ref/contrib/comments/settings`.
  30. Comment template tags
  31. =====================
  32. You'll primarily interact with the comment system through a series of template
  33. tags that let you embed comments and generate forms for your users to post them.
  34. Like all custom template tag libraries, you'll need to :ref:`load the custom
  35. tags <loading-custom-template-libraries>` before you can use them::
  36. {% load comments %}
  37. Once loaded you can use the template tags below.
  38. Specifying which object comments are attached to
  39. ------------------------------------------------
  40. Django's comments are all "attached" to some parent object. This can be any
  41. instance of a Django model. Each of the tags below gives you a couple of
  42. different ways you can specify which object to attach to:
  43. #. Refer to the object directly -- the more common method. Most of the
  44. time, you'll have some object in the template's context you want
  45. to attach the comment to; you can simply use that object.
  46. For example, in a blog entry page that has a variable named ``entry``,
  47. you could use the following to load the number of comments::
  48. {% get_comment_count for entry as comment_count %}.
  49. #. Refer to the object by content-type and object id. You'd use this method
  50. if you, for some reason, don't actually have direct access to the object.
  51. Following the above example, if you knew the object ID was ``14`` but
  52. didn't have access to the actual object, you could do something like::
  53. {% get_comment_count for blog.entry 14 as comment_count %}
  54. In the above, ``blog.entry`` is the app label and (lower-cased) model
  55. name of the model class.
  56. Displaying comments
  57. -------------------
  58. To display a list of comments, you can use the template tags
  59. :ttag:`render_comment_list` or :ttag:`get_comment_list`.
  60. .. templatetag:: render_comment_list
  61. Quickly rendering a comment list
  62. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  63. The easiest way to display a list of comments for some object is by using
  64. :ttag:`render_comment_list`::
  65. {% render_comment_list for [object] %}
  66. For example::
  67. {% render_comment_list for event %}
  68. This will render comments using a template named ``comments/list.html``, a
  69. default version of which is included with Django.
  70. .. templatetag:: get_comment_list
  71. Rendering a custom comment list
  72. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  73. To get the list of comments for some object, use :ttag:`get_comment_list`::
  74. {% get_comment_list for [object] as [varname] %}
  75. For example::
  76. {% get_comment_list for event as comment_list %}
  77. {% for comment in comment_list %}
  78. ...
  79. {% endfor %}
  80. This returns a list of :class:`~django.contrib.comments.models.Comment` objects;
  81. see :doc:`the comment model documentation </ref/contrib/comments/models>` for
  82. details.
  83. .. templatetag:: get_comment_permalink
  84. Linking to comments
  85. -------------------
  86. To provide a permalink to a specific comment, use :ttag:`get_comment_permalink`::
  87. {% get_comment_permalink comment_obj [format_string] %}
  88. By default, the named anchor that will be appended to the URL will be the letter
  89. 'c' followed by the comment id, for example 'c82'. You may specify a custom
  90. format string if you wish to override this behavior::
  91. {% get_comment_permalink comment "#c%(id)s-by-%(user_name)s"%}
  92. The format string is a standard python format string. Valid mapping keys
  93. include any attributes of the comment object.
  94. Regardless of whether you specify a custom anchor pattern, you must supply a
  95. matching named anchor at a suitable place in your template.
  96. For example::
  97. {% for comment in comment_list %}
  98. <a name="c{{ comment.id }}"></a>
  99. <a href="{% get_comment_permalink comment %}">
  100. permalink for comment #{{ forloop.counter }}
  101. </a>
  102. ...
  103. {% endfor %}
  104. .. warning::
  105. There's a `known bug`_ in Safari/Webkit which causes the named anchor to be
  106. forgotten following a redirect. The practical impact for comments is that
  107. the Safari/webkit browsers will arrive at the correct page but will not
  108. scroll to the named anchor.
  109. .. _`known bug`: https://bugs.webkit.org/show_bug.cgi?id=24175
  110. .. templatetag:: get_comment_count
  111. Counting comments
  112. -----------------
  113. To count comments attached to an object, use :ttag:`get_comment_count`::
  114. {% get_comment_count for [object] as [varname] %}
  115. For example::
  116. {% get_comment_count for event as comment_count %}
  117. <p>This event has {{ comment_count }} comments.</p>
  118. Displaying the comment post form
  119. --------------------------------
  120. To show the form that users will use to post a comment, you can use
  121. :ttag:`render_comment_form` or :ttag:`get_comment_form`
  122. .. templatetag:: render_comment_form
  123. Quickly rendering the comment form
  124. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  125. The easiest way to display a comment form is by using
  126. :ttag:`render_comment_form`::
  127. {% render_comment_form for [object] %}
  128. For example::
  129. {% render_comment_form for event %}
  130. This will render comments using a template named ``comments/form.html``, a
  131. default version of which is included with Django.
  132. .. templatetag:: get_comment_form
  133. Rendering a custom comment form
  134. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  135. If you want more control over the look and feel of the comment form, you use use
  136. :ttag:`get_comment_form` to get a :doc:`form object </topics/forms/index>` that
  137. you can use in the template::
  138. {% get_comment_form for [object] as [varname] %}
  139. A complete form might look like::
  140. {% get_comment_form for event as form %}
  141. <table>
  142. <form action="{% comment_form_target %}" method="post">
  143. {% csrf_token %}
  144. {{ form }}
  145. <tr>
  146. <td colspan="2">
  147. <input type="submit" name="submit" value="Post">
  148. <input type="submit" name="preview" value="Preview">
  149. </td>
  150. </tr>
  151. </form>
  152. </table>
  153. Be sure to read the `notes on the comment form`_, below, for some special
  154. considerations you'll need to make if you're using this approach.
  155. .. templatetag:: comment_form_target
  156. Getting the comment form target
  157. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  158. You may have noticed that the above example uses another template tag --
  159. :ttag:`comment_form_target` -- to actually get the ``action`` attribute of the
  160. form. This will always return the correct URL that comments should be posted to;
  161. you'll always want to use it like above::
  162. <form action="{% comment_form_target %}" method="post">
  163. Redirecting after the comment post
  164. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  165. To specify the URL you want to redirect to after the comment has been posted,
  166. you can include a hidden form input called ``next`` in your comment form. For example::
  167. <input type="hidden" name="next" value="{% url 'my_comment_was_posted' %}" />
  168. .. _notes-on-the-comment-form:
  169. Notes on the comment form
  170. -------------------------
  171. The form used by the comment system has a few important anti-spam attributes you
  172. should know about:
  173. * It contains a number of hidden fields that contain timestamps, information
  174. about the object the comment should be attached to, and a "security hash"
  175. used to validate this information. If someone tampers with this data --
  176. something comment spammers will try -- the comment submission will fail.
  177. If you're rendering a custom comment form, you'll need to make sure to
  178. pass these values through unchanged.
  179. * The timestamp is used to ensure that "reply attacks" can't continue very
  180. long. Users who wait too long between requesting the form and posting a
  181. comment will have their submissions refused.
  182. * The comment form includes a "honeypot_" field. It's a trap: if any data is
  183. entered in that field, the comment will be considered spam (spammers often
  184. automatically fill in all fields in an attempt to make valid submissions).
  185. The default form hides this field with a piece of CSS and further labels
  186. it with a warning field; if you use the comment form with a custom
  187. template you should be sure to do the same.
  188. The comments app also depends on the more general :doc:`Cross Site Request
  189. Forgery protection </ref/contrib/csrf>` that comes with Django. As described in
  190. the documentation, it is best to use ``CsrfViewMiddleware``. However, if you
  191. are not using that, you will need to use the ``csrf_protect`` decorator on any
  192. views that include the comment form, in order for those views to be able to
  193. output the CSRF token and cookie.
  194. .. _honeypot: http://en.wikipedia.org/wiki/Honeypot_(computing)
  195. More information
  196. ================
  197. .. toctree::
  198. :maxdepth: 1
  199. models
  200. settings
  201. signals
  202. upgrade
  203. custom
  204. forms
  205. moderation
  206. example