custom.txt 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. ==================================
  2. Customizing the comments framework
  3. ==================================
  4. .. currentmodule:: django.contrib.comments
  5. If the built-in comment framework doesn't quite fit your needs, you can extend
  6. the comment app's behavior to add custom data and logic. The comments framework
  7. lets you extend the built-in comment model, the built-in comment form, and the
  8. various comment views.
  9. The :setting:`COMMENTS_APP` setting is where this customization begins. Set
  10. :setting:`COMMENTS_APP` to the name of the app you'd like to use to provide
  11. custom behavior. You'll use the same syntax as you'd use for
  12. :setting:`INSTALLED_APPS`, and the app given must also be in the
  13. :setting:`INSTALLED_APPS` list.
  14. For example, if you wanted to use an app named ``my_comment_app``, your
  15. settings file would contain::
  16. INSTALLED_APPS = [
  17. ...
  18. 'my_comment_app',
  19. ...
  20. ]
  21. COMMENTS_APP = 'my_comment_app'
  22. The app named in :setting:`COMMENTS_APP` provides its custom behavior by
  23. defining some module-level functions in the app's ``__init__.py``. The
  24. :ref:`complete list of these functions <custom-comment-app-api>` can be found
  25. below, but first let's look at a quick example.
  26. An example custom comments app
  27. ==============================
  28. One of the most common types of customization is modifying the set of fields
  29. provided on the built-in comment model. For example, some sites that allow
  30. comments want the commentator to provide a title for their comment; the built-in
  31. comment model has no field for that title.
  32. To make this kind of customization, we'll need to do three things:
  33. #. Create a custom comment :class:`~django.db.models.Model` that adds on the
  34. "title" field.
  35. #. Create a custom comment :class:`~django.forms.Form` that also adds this
  36. "title" field.
  37. #. Inform Django of these objects by defining a few functions in a
  38. custom :setting:`COMMENTS_APP`.
  39. So, carrying on the example above, we're dealing with a typical app structure in
  40. the ``my_custom_app`` directory::
  41. my_custom_app/
  42. __init__.py
  43. models.py
  44. forms.py
  45. In the ``models.py`` we'll define a ``CommentWithTitle`` model::
  46. from django.db import models
  47. from django.contrib.comments.models import Comment
  48. class CommentWithTitle(Comment):
  49. title = models.CharField(max_length=300)
  50. Most custom comment models will subclass the :class:`Comment` model. However,
  51. if you want to substantially remove or change the fields available in the
  52. :class:`Comment` model, but don't want to rewrite the templates, you could
  53. try subclassing from :class:`BaseCommentAbstractModel`.
  54. Next, we'll define a custom comment form in ``forms.py``. This is a little more
  55. tricky: we have to both create a form and override
  56. :meth:`CommentForm.get_comment_model` and
  57. :meth:`CommentForm.get_comment_create_data` to return deal with our custom title
  58. field::
  59. from django import forms
  60. from django.contrib.comments.forms import CommentForm
  61. from my_comment_app.models import CommentWithTitle
  62. class CommentFormWithTitle(CommentForm):
  63. title = forms.CharField(max_length=300)
  64. def get_comment_model(self):
  65. # Use our custom comment model instead of the built-in one.
  66. return CommentWithTitle
  67. def get_comment_create_data(self):
  68. # Use the data of the superclass, and add in the title field
  69. data = super(CommentFormWithTitle, self).get_comment_create_data()
  70. data['title'] = self.cleaned_data['title']
  71. return data
  72. Django provides a couple of "helper" classes to make writing certain types of
  73. custom comment forms easier; see :mod:`django.contrib.comments.forms` for
  74. more.
  75. Finally, we'll define a couple of methods in ``my_custom_app/__init__.py`` to
  76. point Django at these classes we've created::
  77. from my_comments_app.models import CommentWithTitle
  78. from my_comments_app.forms import CommentFormWithTitle
  79. def get_model():
  80. return CommentWithTitle
  81. def get_form():
  82. return CommentFormWithTitle
  83. The above process should take care of most common situations. For more
  84. advanced usage, there are additional methods you can define. Those are
  85. explained in the next section.
  86. .. _custom-comment-app-api:
  87. Custom comment app API
  88. ======================
  89. The :mod:`django.contrib.comments` app defines the following methods; any
  90. custom comment app must define at least one of them. All are optional,
  91. however.
  92. .. function:: get_model()
  93. Return the :class:`~django.db.models.Model` class to use for comments. This
  94. model should inherit from
  95. :class:`django.contrib.comments.models.BaseCommentAbstractModel`, which
  96. defines necessary core fields.
  97. The default implementation returns
  98. :class:`django.contrib.comments.models.Comment`.
  99. .. function:: get_form()
  100. Return the :class:`~django.forms.Form` class you want to use for
  101. creating, validating, and saving your comment model. Your custom
  102. comment form should accept an additional first argument,
  103. ``target_object``, which is the object the comment will be
  104. attached to.
  105. The default implementation returns
  106. :class:`django.contrib.comments.forms.CommentForm`.
  107. .. note::
  108. The default comment form also includes a number of unobtrusive
  109. spam-prevention features (see
  110. :ref:`notes-on-the-comment-form`). If replacing it with your
  111. own form, you may want to look at the source code for the
  112. built-in form and consider incorporating similar features.
  113. .. function:: get_form_target()
  114. Return the URL for POSTing comments. This will be the ``<form action>``
  115. attribute when rendering your comment form.
  116. The default implementation returns a reverse-resolved URL pointing
  117. to the :func:`post_comment` view.
  118. .. note::
  119. If you provide a custom comment model and/or form, but you
  120. want to use the default :func:`post_comment` view, you will
  121. need to be aware that it requires the model and form to have
  122. certain additional attributes and methods: see the
  123. :func:`post_comment` view documentation for details.
  124. .. function:: get_flag_url()
  125. Return the URL for the "flag this comment" view.
  126. The default implementation returns a reverse-resolved URL pointing
  127. to the :func:`django.contrib.comments.views.moderation.flag` view.
  128. .. function:: get_delete_url()
  129. Return the URL for the "delete this comment" view.
  130. The default implementation returns a reverse-resolved URL pointing
  131. to the :func:`django.contrib.comments.views.moderation.delete` view.
  132. .. function:: get_approve_url()
  133. Return the URL for the "approve this comment from moderation" view.
  134. The default implementation returns a reverse-resolved URL pointing
  135. to the :func:`django.contrib.comments.views.moderation.approve` view.