.. highlightlang:: html+django =========================================== Example of using the built-in comments app =========================================== .. warning:: Django's comment framework has been deprecated and is no longer supported. Most users will be better served with a custom solution, or a hosted product like Disqus__. The code formerly known as ``django.contrib.comments`` is `still available in an external repository`__. __ https://disqus.com/ __ https://github.com/django/django-contrib-comments Follow the first three steps of the quick start guide in the :doc:`documentation `. Now suppose, you have an app (``blog``) with a model (``Post``) to which you want to attach comments. Let's also suppose that you have a template called ``blog_detail.html`` where you want to display the comments list and comment form. Template ======== First, we should load the ``comment`` template tags in the ``blog_detail.html`` so that we can use its functionality. So just like all other custom template tag libraries:: {% load comments %} Next, let's add the number of comments attached to the particular model instance of ``Post``. For this we assume that a context variable ``object_pk`` is present which gives the ``id`` of the instance of ``Post``. The usage of the :ttag:`get_comment_count` tag is like below:: {% get_comment_count for blog.post object_pk as comment_count %}
{{ comment_count }} comments have been posted.
If you have the instance (say ``entry``) of the model (``Post``) available in the context, then you can refer to it directly:: {% get_comment_count for entry as comment_count %}{{ comment_count }} comments have been posted.
Next, we can use the :ttag:`render_comment_list` tag, to render all comments to the given instance (``entry``) by using the ``comments/list.html`` template:: {% render_comment_list for entry %} Django will will look for the ``list.html`` under the following directories (for our example):: comments/blog/post/list.html comments/blog/list.html comments/list.html To get a list of comments, we make use of the :ttag:`get_comment_list` tag. Using this tag is very similar to the :ttag:`get_comment_count` tag. We need to remember that :ttag:`get_comment_list` returns a list of comments and hence we have to iterate through them to display them:: {% get_comment_list for blog.post object_pk as comment_list %} {% for comment in comment_list %}Posted by: {{ comment.user_name }} on {{ comment.submit_date }}
...Comment: {{ comment.comment }}
... {% endfor %} Finally, we display the comment form, enabling users to enter their comments. There are two ways of doing so. The first is when you want to display the comments template available under your ``comments/form.html``. The other method gives you a chance to customize the form. The first method makes use of the :ttag:`render_comment_form` tag. Its usage too is similar to the other three tags we have discussed above:: {% render_comment_form for entry %} It looks for the ``form.html`` under the following directories (for our example):: comments/blog/post/form.html comments/blog/form.html comments/form.html Since we customize the form in the second method, we make use of another tag called :ttag:`comment_form_target`. This tag on rendering gives the URL where the comment form is posted. Without any :doc:`customization `, :ttag:`comment_form_target` evaluates to ``/comments/post/``. We use this tag in the form's ``action`` attribute. The :ttag:`get_comment_form` tag renders a ``form`` for a model instance by creating a context variable. One can iterate over the ``form`` object to get individual fields. This gives you fine-grain control over the form:: {% for field in form %} {% ifequal field.name "comment" %} ... {% endfor %} But let's look at a simple example:: {% get_comment_form for entry as form %}