index.txt 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. .. _ref-contrib-comments-index:
  2. ===========================
  3. Django's comments framework
  4. ===========================
  5. .. module:: django.contrib.comments
  6. :synopsis: Django's comment framework
  7. .. highlightlang:: html+django
  8. Django includes a simple, yet customizable comments framework. The built-in
  9. comments framework can be used to attach comments to any model, so you can use
  10. it for comments on blog entries, photos, book chapters, or anything else.
  11. .. note::
  12. If you used to use Django's older (undocumented) comments framework, you'll
  13. need to upgrade. See the :ref:`upgrade guide <ref-contrib-comments-upgrade>`
  14. for instructions.
  15. Quick start guide
  16. =================
  17. To get started using the ``comments`` app, follow these steps:
  18. #. Install the comments framework by adding ``'django.contrib.comments'`` to
  19. :setting:`INSTALLED_APPS`.
  20. #. Run ``manage.py syncdb`` so that Django will create the comment tables.
  21. #. Add the comment app's URLs to your project's ``urls.py``:
  22. .. code-block:: python
  23. urlpatterns = patterns('',
  24. ...
  25. (r'^comments/', include('django.contrib.comments.urls')),
  26. ...
  27. )
  28. #. Use the `comment template tags`_ below to embed comments in your
  29. templates.
  30. You might also want to examine the :ref:`ref-contrib-comments-settings`
  31. Comment template tags
  32. =====================
  33. You'll primarily interact with the comment system through a series of template
  34. tags that let you embed comments and generate forms for your users to post them.
  35. Like all custom template tag libraries, you'll need to :ref:`load the custom
  36. tags <loading-custom-template-libraries>` before you can use them::
  37. {% load comments %}
  38. Once loaded you can use the template tags below.
  39. Specifying which object comments are attached to
  40. ------------------------------------------------
  41. Django's comments are all "attached" to some parent object. This can be any
  42. instance of a Django model. Each of the tags below gives you a couple of
  43. different ways you can specify which object to attach to:
  44. #. Refer to the object directly -- the more common method. Most of the
  45. time, you'll have some object in the template's context you want
  46. to attach the comment to; you can simply use that object.
  47. For example, in a blog entry page that has a variable named ``entry``,
  48. you could use the following to load the number of comments::
  49. {% get_comment_count for entry as comment_count %}.
  50. #. Refer to the object by content-type and object id. You'd use this method
  51. if you, for some reason, don't actually have direct access to the object.
  52. Following the above example, if you knew the object ID was ``14`` but
  53. didn't have access to the actual object, you could do something like::
  54. {% get_comment_count for blog.entry 14 as comment_count %}
  55. In the above, ``blog.entry`` is the app label and (lower-cased) model
  56. name of the model class.
  57. .. templatetag:: get_comment_list
  58. Displaying comments
  59. -------------------
  60. To get a the list of comments for some object, use :ttag:`get_comment_list`::
  61. {% get_comment_list for [object] as [varname] %}
  62. For example::
  63. {% get_comment_list for event as comment_list %}
  64. {% for comment in comment_list %}
  65. ...
  66. {% endfor %}
  67. .. templatetag:: get_comment_count
  68. Counting comments
  69. -----------------
  70. To count comments attached to an object, use :ttag:`get_comment_count`::
  71. {% get_comment_count for [object] as [varname] %}
  72. For example::
  73. {% get_comment_count for event as comment_count %}
  74. <p>This event has {{ comment_count }} comments.</p>
  75. Displaying the comment post form
  76. --------------------------------
  77. To show the form that users will use to post a comment, you can use
  78. :ttag:`render_comment_form` or :ttag:`get_comment_form`
  79. .. templatetag:: render_comment_form
  80. Quickly rendering the comment form
  81. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  82. The easiest way to display a comment form is by using
  83. :ttag:`render_comment_form`::
  84. {% render_comment_form for [object] %}
  85. For example::
  86. {% render_comment_form for event %}
  87. This will render comments using a template named ``comments/form.html``, a
  88. default version of which is included with Django.
  89. .. templatetag:: get_comment_form
  90. Rendering a custom comment form
  91. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  92. If you want more control over the look and feel of the comment form, you use use
  93. :ttag:`get_comment_form` to get a :ref:`form object <topics-forms-index>` that
  94. you can use in the template::
  95. {% get_comment_form for [object] as [varname] %}
  96. A complete form might look like::
  97. {% get_comment_form for event as form %}
  98. <form action="{% comment_form_target %}" method="POST">
  99. {{ form }}
  100. <p class="submit">
  101. <input type="submit" name="submit" class="submit-post" value="Preview">
  102. </p>
  103. </form>
  104. Be sure to read the `notes on the comment form`_, below, for some special
  105. considerations you'll need to make if you're using this aproach.
  106. .. templatetag:: comment_form_target
  107. Getting the comment form target
  108. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  109. You may have noticed that the above example uses another template tag --
  110. :ttag:`comment_form_target` -- to actually get the ``action`` attribute of the
  111. form. This will always return the correct URL that comments should be posted to;
  112. you'll always want to use it like above::
  113. <form action="{% comment_form_target %}" method="POST">
  114. Notes on the comment form
  115. -------------------------
  116. The form used by the comment system has a few important anti-spam attributes you
  117. should know about:
  118. * It contains a number of hidden fields that contain timestamps, information
  119. about the object the comment should be attached to, and a "security hash"
  120. used to validate this information. If someone tampers with this data --
  121. something comment spammers will try -- the comment submission will fail.
  122. If you're rendering a custom comment form, you'll need to make sure to
  123. pass these values through unchanged.
  124. * The timestamp is used to ensure that "reply attacks" can't continue very
  125. long. Users who wait too long between requesting the form and posting a
  126. comment will have their submissions refused.
  127. * The comment form includes a "honeypot_" field. It's a trap: if any data is
  128. entered in that field, the comment will be considered spam (spammers often
  129. automatically fill in all fields in an attempt to make valid submissions).
  130. The default form hides this field with a piece of CSS and further labels
  131. it with a warning field; if you use the comment form with a custom
  132. template you should be sure to do the same.
  133. .. _honeypot: http://en.wikipedia.org/wiki/Honeypot_(computing)
  134. More information
  135. ================
  136. .. toctree::
  137. :maxdepth: 1
  138. settings
  139. signals
  140. upgrade