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