2008-08-26 06:14:22 +08:00
|
|
|
===========================
|
|
|
|
Django's comments framework
|
|
|
|
===========================
|
|
|
|
|
|
|
|
.. module:: django.contrib.comments
|
|
|
|
:synopsis: Django's comment framework
|
|
|
|
|
2008-09-09 09:54:20 +08:00
|
|
|
.. highlightlang:: html+django
|
|
|
|
|
2008-08-26 06:14:22 +08:00
|
|
|
Django includes a simple, yet customizable comments framework. The built-in
|
|
|
|
comments framework can be used to attach comments to any model, so you can use
|
|
|
|
it for comments on blog entries, photos, book chapters, or anything else.
|
|
|
|
|
|
|
|
.. note::
|
|
|
|
|
|
|
|
If you used to use Django's older (undocumented) comments framework, you'll
|
2010-08-20 03:27:44 +08:00
|
|
|
need to upgrade. See the :doc:`upgrade guide </ref/contrib/comments/upgrade>`
|
2008-08-26 06:14:22 +08:00
|
|
|
for instructions.
|
|
|
|
|
|
|
|
Quick start guide
|
|
|
|
=================
|
|
|
|
|
|
|
|
To get started using the ``comments`` app, follow these steps:
|
|
|
|
|
2009-09-13 06:43:20 +08:00
|
|
|
#. Install the comments framework by adding ``'django.contrib.comments'`` to
|
2008-08-26 06:14:22 +08:00
|
|
|
:setting:`INSTALLED_APPS`.
|
|
|
|
|
|
|
|
#. Run ``manage.py syncdb`` so that Django will create the comment tables.
|
|
|
|
|
|
|
|
#. Add the comment app's URLs to your project's ``urls.py``:
|
2009-09-13 06:43:20 +08:00
|
|
|
|
2008-08-26 06:14:22 +08:00
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
urlpatterns = patterns('',
|
|
|
|
...
|
|
|
|
(r'^comments/', include('django.contrib.comments.urls')),
|
|
|
|
...
|
|
|
|
)
|
|
|
|
|
|
|
|
#. Use the `comment template tags`_ below to embed comments in your
|
|
|
|
templates.
|
2009-09-13 06:43:20 +08:00
|
|
|
|
2010-08-20 03:27:44 +08:00
|
|
|
You might also want to examine :doc:`/ref/contrib/comments/settings`.
|
2009-09-13 06:43:20 +08:00
|
|
|
|
2008-08-26 06:14:22 +08:00
|
|
|
Comment template tags
|
|
|
|
=====================
|
|
|
|
|
|
|
|
You'll primarily interact with the comment system through a series of template
|
|
|
|
tags that let you embed comments and generate forms for your users to post them.
|
|
|
|
|
|
|
|
Like all custom template tag libraries, you'll need to :ref:`load the custom
|
|
|
|
tags <loading-custom-template-libraries>` before you can use them::
|
|
|
|
|
|
|
|
{% load comments %}
|
|
|
|
|
|
|
|
Once loaded you can use the template tags below.
|
|
|
|
|
|
|
|
Specifying which object comments are attached to
|
|
|
|
------------------------------------------------
|
|
|
|
|
|
|
|
Django's comments are all "attached" to some parent object. This can be any
|
|
|
|
instance of a Django model. Each of the tags below gives you a couple of
|
|
|
|
different ways you can specify which object to attach to:
|
|
|
|
|
|
|
|
#. Refer to the object directly -- the more common method. Most of the
|
|
|
|
time, you'll have some object in the template's context you want
|
|
|
|
to attach the comment to; you can simply use that object.
|
2009-09-13 06:43:20 +08:00
|
|
|
|
|
|
|
For example, in a blog entry page that has a variable named ``entry``,
|
2008-08-26 06:14:22 +08:00
|
|
|
you could use the following to load the number of comments::
|
2009-09-13 06:43:20 +08:00
|
|
|
|
2008-08-26 06:14:22 +08:00
|
|
|
{% get_comment_count for entry as comment_count %}.
|
2009-09-13 06:43:20 +08:00
|
|
|
|
2008-08-26 06:14:22 +08:00
|
|
|
#. Refer to the object by content-type and object id. You'd use this method
|
|
|
|
if you, for some reason, don't actually have direct access to the object.
|
2009-09-13 06:43:20 +08:00
|
|
|
|
2008-08-26 06:14:22 +08:00
|
|
|
Following the above example, if you knew the object ID was ``14`` but
|
|
|
|
didn't have access to the actual object, you could do something like::
|
2009-09-13 06:43:20 +08:00
|
|
|
|
2008-08-26 06:14:22 +08:00
|
|
|
{% get_comment_count for blog.entry 14 as comment_count %}
|
2009-09-13 06:43:20 +08:00
|
|
|
|
2008-08-26 06:14:22 +08:00
|
|
|
In the above, ``blog.entry`` is the app label and (lower-cased) model
|
|
|
|
name of the model class.
|
|
|
|
|
|
|
|
Displaying comments
|
|
|
|
-------------------
|
|
|
|
|
2010-01-04 10:29:12 +08:00
|
|
|
To display a list of comments, you can use the template tags
|
|
|
|
:ttag:`render_comment_list` or :ttag:`get_comment_list`.
|
|
|
|
|
|
|
|
.. templatetag:: render_comment_list
|
|
|
|
|
|
|
|
Quickly rendering a comment list
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
The easiest way to display a list of comments for some object is by using
|
|
|
|
:ttag:`render_comment_list`::
|
|
|
|
|
|
|
|
{% render_comment_list for [object] %}
|
|
|
|
|
|
|
|
For example::
|
|
|
|
|
|
|
|
{% render_comment_list for event %}
|
|
|
|
|
|
|
|
This will render comments using a template named ``comments/list.html``, a
|
|
|
|
default version of which is included with Django.
|
|
|
|
|
|
|
|
.. templatetag:: get_comment_list
|
|
|
|
|
|
|
|
Rendering a custom comment list
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
2009-09-13 06:43:20 +08:00
|
|
|
To get the list of comments for some object, use :ttag:`get_comment_list`::
|
2008-08-26 06:14:22 +08:00
|
|
|
|
|
|
|
{% get_comment_list for [object] as [varname] %}
|
|
|
|
|
|
|
|
For example::
|
|
|
|
|
|
|
|
{% get_comment_list for event as comment_list %}
|
|
|
|
{% for comment in comment_list %}
|
|
|
|
...
|
|
|
|
{% endfor %}
|
2009-09-13 06:43:20 +08:00
|
|
|
|
Fixed a whole bunch of small docs typos, errors, and ommissions.
Fixes #8358, #8396, #8724, #9043, #9128, #9247, #9267, #9267, #9375, #9409, #9414, #9416, #9446, #9454, #9464, #9503, #9518, #9533, #9657, #9658, #9683, #9733, #9771, #9835, #9836, #9837, #9897, #9906, #9912, #9945, #9986, #9992, #10055, #10084, #10091, #10145, #10245, #10257, #10309, #10358, #10359, #10424, #10426, #10508, #10531, #10551, #10635, #10637, #10656, #10658, #10690, #10699, #19528.
Thanks to all the respective authors of those tickets.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@10371 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2009-04-04 02:30:54 +08:00
|
|
|
This returns a list of :class:`~django.contrib.comments.models.Comment` objects;
|
2010-08-20 03:27:44 +08:00
|
|
|
see :doc:`the comment model documentation </ref/contrib/comments/models>` for
|
Fixed a whole bunch of small docs typos, errors, and ommissions.
Fixes #8358, #8396, #8724, #9043, #9128, #9247, #9267, #9267, #9375, #9409, #9414, #9416, #9446, #9454, #9464, #9503, #9518, #9533, #9657, #9658, #9683, #9733, #9771, #9835, #9836, #9837, #9897, #9906, #9912, #9945, #9986, #9992, #10055, #10084, #10091, #10145, #10245, #10257, #10309, #10358, #10359, #10424, #10426, #10508, #10531, #10551, #10635, #10637, #10656, #10658, #10690, #10699, #19528.
Thanks to all the respective authors of those tickets.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@10371 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2009-04-04 02:30:54 +08:00
|
|
|
details.
|
2008-08-26 06:14:22 +08:00
|
|
|
|
2010-01-04 10:28:09 +08:00
|
|
|
.. templatetag:: get_comment_permalink
|
|
|
|
|
|
|
|
Linking to comments
|
|
|
|
-------------------
|
|
|
|
|
2010-01-18 06:29:50 +08:00
|
|
|
.. versionadded:: 1.2
|
|
|
|
|
2010-01-04 10:28:09 +08:00
|
|
|
To provide a permalink to a specific comment, use :ttag:`get_comment_permalink`::
|
|
|
|
|
|
|
|
{% get_comment_permalink comment_obj [format_string] %}
|
|
|
|
|
|
|
|
By default, the named anchor that will be appended to the URL will be the letter
|
|
|
|
'c' followed by the comment id, for example 'c82'. You may specify a custom
|
|
|
|
format string if you wish to override this behavior::
|
|
|
|
|
|
|
|
{% get_comment_permalink comment "#c%(id)s-by-%(user_name)s"%}
|
|
|
|
|
|
|
|
The format string is a standard python format string. Valid mapping keys
|
|
|
|
include any attributes of the comment object.
|
|
|
|
|
|
|
|
Regardless of whether you specify a custom anchor pattern, you must supply a
|
|
|
|
matching named anchor at a suitable place in your template.
|
|
|
|
|
|
|
|
For example::
|
|
|
|
|
|
|
|
{% for comment in comment_list %}
|
|
|
|
<a name="c{{ comment.id }}"></a>
|
|
|
|
<a href="{% get_comment_permalink comment %}">
|
|
|
|
permalink for comment #{{ forloop.counter }}
|
|
|
|
</a>
|
|
|
|
...
|
|
|
|
{% endfor %}
|
|
|
|
|
|
|
|
.. warning::
|
|
|
|
|
|
|
|
There's a known bug in Safari/Webkit which causes the named anchor to be
|
|
|
|
forgotten following a redirect. The practical impact for comments is that
|
|
|
|
the Safari/webkit browsers will arrive at the correct page but will not
|
|
|
|
scroll to the named anchor.
|
|
|
|
|
2008-08-26 06:14:22 +08:00
|
|
|
.. templatetag:: get_comment_count
|
|
|
|
|
|
|
|
Counting comments
|
|
|
|
-----------------
|
|
|
|
|
|
|
|
To count comments attached to an object, use :ttag:`get_comment_count`::
|
|
|
|
|
|
|
|
{% get_comment_count for [object] as [varname] %}
|
|
|
|
|
|
|
|
For example::
|
|
|
|
|
|
|
|
{% get_comment_count for event as comment_count %}
|
2009-09-13 06:43:20 +08:00
|
|
|
|
2008-08-26 06:14:22 +08:00
|
|
|
<p>This event has {{ comment_count }} comments.</p>
|
2009-09-13 06:43:20 +08:00
|
|
|
|
2008-08-26 06:14:22 +08:00
|
|
|
|
|
|
|
Displaying the comment post form
|
|
|
|
--------------------------------
|
|
|
|
|
|
|
|
To show the form that users will use to post a comment, you can use
|
|
|
|
:ttag:`render_comment_form` or :ttag:`get_comment_form`
|
|
|
|
|
|
|
|
.. templatetag:: render_comment_form
|
|
|
|
|
|
|
|
Quickly rendering the comment form
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
The easiest way to display a comment form is by using
|
|
|
|
:ttag:`render_comment_form`::
|
|
|
|
|
|
|
|
{% render_comment_form for [object] %}
|
|
|
|
|
|
|
|
For example::
|
|
|
|
|
|
|
|
{% render_comment_form for event %}
|
|
|
|
|
|
|
|
This will render comments using a template named ``comments/form.html``, a
|
|
|
|
default version of which is included with Django.
|
|
|
|
|
|
|
|
.. templatetag:: get_comment_form
|
|
|
|
|
|
|
|
Rendering a custom comment form
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
If you want more control over the look and feel of the comment form, you use use
|
2010-08-20 03:27:44 +08:00
|
|
|
:ttag:`get_comment_form` to get a :doc:`form object </topics/forms/index>` that
|
2008-08-26 06:14:22 +08:00
|
|
|
you can use in the template::
|
|
|
|
|
2008-08-26 23:23:02 +08:00
|
|
|
{% get_comment_form for [object] as [varname] %}
|
2009-09-13 06:43:20 +08:00
|
|
|
|
2008-08-26 06:14:22 +08:00
|
|
|
A complete form might look like::
|
|
|
|
|
2008-08-26 23:23:02 +08:00
|
|
|
{% get_comment_form for event as form %}
|
2011-06-17 00:34:02 +08:00
|
|
|
<table>
|
|
|
|
<form action="{% comment_form_target %}" method="post">
|
|
|
|
{% csrf_token %}
|
|
|
|
{{ form }}
|
|
|
|
<tr>
|
|
|
|
<td colspan="2">
|
|
|
|
<input type="submit" name="submit" value="Post">
|
|
|
|
<input type="submit" name="preview" value="Preview">
|
|
|
|
</td>
|
|
|
|
</tr>
|
|
|
|
</form>
|
|
|
|
</table>
|
2009-09-13 06:43:20 +08:00
|
|
|
|
2008-08-26 06:14:22 +08:00
|
|
|
Be sure to read the `notes on the comment form`_, below, for some special
|
2009-02-24 06:16:26 +08:00
|
|
|
considerations you'll need to make if you're using this approach.
|
2008-08-26 06:14:22 +08:00
|
|
|
|
|
|
|
.. templatetag:: comment_form_target
|
|
|
|
|
|
|
|
Getting the comment form target
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
You may have noticed that the above example uses another template tag --
|
|
|
|
:ttag:`comment_form_target` -- to actually get the ``action`` attribute of the
|
|
|
|
form. This will always return the correct URL that comments should be posted to;
|
|
|
|
you'll always want to use it like above::
|
|
|
|
|
2010-01-05 05:55:52 +08:00
|
|
|
<form action="{% comment_form_target %}" method="post">
|
2008-08-26 06:14:22 +08:00
|
|
|
|
2009-04-08 03:28:13 +08:00
|
|
|
Redirecting after the comment post
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
To specify the URL you want to redirect to after the comment has been posted,
|
|
|
|
you can include a hidden form input called ``next`` in your comment form. For example::
|
2009-09-13 06:43:20 +08:00
|
|
|
|
2009-04-08 03:28:13 +08:00
|
|
|
<input type="hidden" name="next" value="{% url my_comment_was_posted %}" />
|
|
|
|
|
2009-02-24 06:16:26 +08:00
|
|
|
.. _notes-on-the-comment-form:
|
|
|
|
|
2008-08-26 06:14:22 +08:00
|
|
|
Notes on the comment form
|
|
|
|
-------------------------
|
|
|
|
|
|
|
|
The form used by the comment system has a few important anti-spam attributes you
|
|
|
|
should know about:
|
|
|
|
|
|
|
|
* It contains a number of hidden fields that contain timestamps, information
|
|
|
|
about the object the comment should be attached to, and a "security hash"
|
2009-09-13 06:43:20 +08:00
|
|
|
used to validate this information. If someone tampers with this data --
|
2008-08-26 06:14:22 +08:00
|
|
|
something comment spammers will try -- the comment submission will fail.
|
2009-09-13 06:43:20 +08:00
|
|
|
|
2008-08-26 06:14:22 +08:00
|
|
|
If you're rendering a custom comment form, you'll need to make sure to
|
|
|
|
pass these values through unchanged.
|
2009-09-13 06:43:20 +08:00
|
|
|
|
2008-08-26 06:14:22 +08:00
|
|
|
* The timestamp is used to ensure that "reply attacks" can't continue very
|
|
|
|
long. Users who wait too long between requesting the form and posting a
|
|
|
|
comment will have their submissions refused.
|
2009-09-13 06:43:20 +08:00
|
|
|
|
2008-08-26 06:14:22 +08:00
|
|
|
* The comment form includes a "honeypot_" field. It's a trap: if any data is
|
|
|
|
entered in that field, the comment will be considered spam (spammers often
|
|
|
|
automatically fill in all fields in an attempt to make valid submissions).
|
2009-09-13 06:43:20 +08:00
|
|
|
|
2008-08-26 06:14:22 +08:00
|
|
|
The default form hides this field with a piece of CSS and further labels
|
|
|
|
it with a warning field; if you use the comment form with a custom
|
|
|
|
template you should be sure to do the same.
|
2009-09-13 06:43:20 +08:00
|
|
|
|
2010-08-20 03:27:44 +08:00
|
|
|
The comments app also depends on the more general :doc:`Cross Site Request
|
|
|
|
Forgery protection </ref/contrib/csrf>` that comes with Django. As described in
|
2009-11-03 22:40:37 +08:00
|
|
|
the documentation, it is best to use ``CsrfViewMiddleware``. However, if you
|
|
|
|
are not using that, you will need to use the ``csrf_protect`` decorator on any
|
|
|
|
views that include the comment form, in order for those views to be able to
|
|
|
|
output the CSRF token and cookie.
|
|
|
|
|
2008-08-26 06:14:22 +08:00
|
|
|
.. _honeypot: http://en.wikipedia.org/wiki/Honeypot_(computing)
|
|
|
|
|
|
|
|
More information
|
|
|
|
================
|
|
|
|
|
|
|
|
.. toctree::
|
|
|
|
:maxdepth: 1
|
|
|
|
|
Fixed a whole bunch of small docs typos, errors, and ommissions.
Fixes #8358, #8396, #8724, #9043, #9128, #9247, #9267, #9267, #9375, #9409, #9414, #9416, #9446, #9454, #9464, #9503, #9518, #9533, #9657, #9658, #9683, #9733, #9771, #9835, #9836, #9837, #9897, #9906, #9912, #9945, #9986, #9992, #10055, #10084, #10091, #10145, #10245, #10257, #10309, #10358, #10359, #10424, #10426, #10508, #10531, #10551, #10635, #10637, #10656, #10658, #10690, #10699, #19528.
Thanks to all the respective authors of those tickets.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@10371 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2009-04-04 02:30:54 +08:00
|
|
|
models
|
2008-08-26 06:14:22 +08:00
|
|
|
settings
|
2008-08-27 03:04:52 +08:00
|
|
|
signals
|
2008-08-26 06:14:22 +08:00
|
|
|
upgrade
|
2009-02-24 06:16:26 +08:00
|
|
|
custom
|
2009-03-21 21:45:31 +08:00
|
|
|
forms
|
2009-09-13 06:43:20 +08:00
|
|
|
moderation
|
2010-01-04 10:27:37 +08:00
|
|
|
example
|