Django - Comments
Django - Comments Framework
Section titled “Django - Comments Framework”Important Note: The built-in django.contrib.comments framework, which was part of Django core, was deprecated in Django 1.6 and completely removed in Django 1.8. It is no longer included with Django.
While the original framework is gone, the need for comment systems remains. You have several options:
- Use a Third-Party Package: The community maintains a package called
django-contrib-commentswhich provides similar functionality to the old framework. This is often the easiest way to get a familiar comment system up and running. - Use an External Service: Integrate a hosted commenting platform like Disqus or Commento. These services handle the comment backend for you, often simplifying setup and moderation.
- Build Your Own: For specific needs or full control, you can build a custom commenting system using Django models, forms, and views. This offers maximum flexibility but requires more development effort.
Using django-contrib-comments (Third-Party Package)
Section titled “Using django-contrib-comments (Third-Party Package)”This section demonstrates how to use the popular django-contrib-comments package, which mirrors the old framework’s functionality.
- Installation:
pip install django-contrib-comments- Configuration (
settings.py):
# Add 'django_comments' and 'django.contrib.sites' to INSTALLED_APPSINSTALLED_APPS = [ # ... other apps 'django.contrib.sites', 'django_comments', 'myapp', # Your app with models you want to comment on # ...]
# Add a SITE_ID setting. The sites framework requires this.# Typically, 1 is used for the primary site.SITE_ID = 1The django.contrib.sites framework allows associating data (like comments) with specific “sites” if your Django project serves multiple websites. For a single site, setting SITE_ID = 1 is standard.
- Database Migrations:
# Create the necessary database tables for comments and sitespython manage.py migrate- Include URLs (
urls.py):
# In your main project urls.py (e.g., myproject/urls.py)from django.contrib import adminfrom django.urls import path, include
urlpatterns = [ path('admin/', admin.site.urls), path('myapp/', include('myapp.urls')), # Your app's URLs path('comments/', include('django_comments.urls')), # Add the comments app URLs]- Using Comments in Templates:
Let’s assume you have a myapp.models.Article model and want to add comments to its detail page (article_detail.html).
from django.shortcuts import render, get_object_or_404from .models import Article
def article_detail_view(request, pk): article = get_object_or_404(Article, pk=pk) return render(request, 'myapp/article_detail.html', {'article': article}){% extends "base.html" %}{% load comments %}
{% block title %}{{ article.title }}{% endblock %}
{% block content %} <h1>{{ article.title }}</h1> <p>{{ article.content }}</p>
<hr> <h2>Comments</h2>
{# Display comment count #} {% get_comment_count for article as comment_count %} <p>There {{ comment_count|pluralize:"is,are" }} {{ comment_count }} comment{{ comment_count|pluralize }} for this article.</p>
{# Display the comment form #} <h3>Post a comment:</h3> {% render_comment_form for article %}
{# Display the list of comments #} <h3>Existing comments:</h3> {% render_comment_list for article %}
{% endblock %}Explanation of Template Tags:
{% load comments %}: Loads the necessary template tags.{% get_comment_count for [object] as [varname] %}: Gets the number of comments for the specified model instance (articlein this case) and stores it in thecomment_countvariable.{% render_comment_form for [object] %}: Renders the default form for posting a comment related to the object.{% render_comment_list for [object] %}: Renders the list of approved comments associated with the object.
After setting this up, visiting an article’s detail page will show the comment count, the comment form, and any existing comments.
Moderation and Customization
Section titled “Moderation and Customization”django-contrib-comments supports comment moderation (requiring approval before comments appear), email notifications, and customization of forms and templates. Refer to the package’s official documentation for details on these advanced features.
Alternatives
Section titled “Alternatives”If django-contrib-comments doesn’t fit your needs:
- Disqus/Commento: Easy to integrate, offloads comment hosting and management. Often includes features like social login, advanced moderation, and analytics. Requires users to potentially interact with a third-party service.
- Build Your Own: Create a
Commentmodel in Django with aForeignKeyto the content type it relates to (usingdjango.contrib.contenttypes), and possibly a user. Build forms and views to handle creation and display. Provides complete control but takes time.
Choose the approach that best balances ease of use, required features, and development resources for your project.
Further Reading:
django-contrib-commentsDocumentation: https://django-contrib-comments.readthedocs.io/- Django ContentTypes Framework: https://docs.djangoproject.com/en/stable/ref/contrib/contenttypes/ (Useful for building your own system)