Django’s comments framework – Free Django Tutorials

Django have a simple and customizable comments framework for it’s users.We can attach comments to any model using this built-in comments framework in django.For example we can use it for comments on any blog or photos.

You can follow these steps,if you want to get started with comments app:

  • You can first install the comments framework inside your project by adding ‘django.contrib.comments‘ to the INSTALLED_APPS list in your settings.py.
  • After adding the comments framework you can run python manage.py migrate,which will create the tables of comment inside your database.
  • After all this add the comment app’s URL to your main urls.py file as following to include comments app in your project.
urlpatterns = patterns('',
    ...
    (r'^comments/', include('django.contrib.comments.urls')),
    ...
)

Comment template tags

For interacting with the comment system inside your django project you can use a series of template tags which will let you embed comments and generate forms for your users to post them.

If you want  to use custom tag inside your project,so for that you have to load it like this :

{% load comments %}

Once you have loaded the custom tag by adding the above line,then you can use it anywhere in your project.

Specifying which object comments are attached to

All of the comments that we write in django are attached to some parent object.This parent object can be any instance of a django model.In the following few examples we are showing a couple of different ways you can specify which object to attach to:

  1. The most common way to attach is you can refer to the object directly. Most of the time the object which you want to attach the comment to is available in the template’s context itself. For example, you could use the following to load the number of comments in a blog entry page that has variable name entry.

    {% get_comment_count for entry as comment_count %}.
    

    2. The second method to refer to parent object is by content-type and object-id of object.You can use this method whenevar in the project you don’t actually have direct access to the object. For example in the above given example if you have a object ID a 23 then you could do something like this:

    {% get_comment_count for blog.entry 23 as comment_count %}
    

Displaying comments

You can use the template tags render_comment_list or get_comment_list to display a list of comments.

Quickly rendering a comment list

For some object you can display a list of comments quickly by using render_comment_list as following:

{% render_comment_list for [object] %}

For example:

{% render_comment_list for event %}

Linking to comments

You can use get_comment_permalink to provide a permalink to a specific comment as following:

{% get_comment_permalink comment_obj [format_string] %}

By default django will append the letter ‘c’ as a named anchor followed by the comment id at the end of URL.for example ‘c76’

{% get_comment_permalink comment "#c%(id)s-by-%(user_name)s"%}

You must supply a matching named anchor at a suitable place in your template,regardless of weather you specify a custom anchor pattern.

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 %}

Counting comments

You can use get_comment_count to count the total comments attached to an object.

{% get_comment_count for [object] as [varname]  %}

For example:

{% get_comment_count for event as comment_count %}

<p>This event has {{ comment_count }} comments.</p>

So this is all about comments framework in django and how to use it in your project.

Leave a Comment

Your email address will not be published. Required fields are marked *