Django Contrib Comments
django-contrib-comments is a standalone Django application providing a robust system for handling user comments. It was extracted from Django core as of version 1.6 and is actively maintained. The current stable version is 2.2.0, with releases typically aligning with major Django versions.
Common errors
-
ModuleNotFoundError: No module named 'django.contrib.comments'
cause Attempting to import from the old Django core comments module, which was removed in Django 1.6.fixInstall `django-contrib-comments` (`pip install django-contrib-comments`) and change all imports from `django.contrib.comments` to `django_comments` (e.g., `from django_comments.models import Comment`). -
django.db.utils.OperationalError: no such table: django_comments_comment
cause The database tables for `django_comments` have not been created or migrations have not been run.fixEnsure `'django_comments'` is in `INSTALLED_APPS` and run `python manage.py migrate`. -
django.core.exceptions.ImproperlyConfigured: You're using the Django "sites" framework without having defined the SITE_ID setting.
cause `django-contrib-comments` requires `django.contrib.sites` and a `SITE_ID` setting.fixAdd `'django.contrib.sites'` to `INSTALLED_APPS` and `SITE_ID = 1` (or your site's ID) to your `settings.py`. -
django.urls.exceptions.NoReverseMatch: Reverse for 'comments-post-comment' not found. 'comments-post-comment' is not a registered namespace.
cause The URL patterns for `django_comments` have not been included in your project's `urls.py`.fixAdd `path('comments/', include('django_comments.urls'))` to your project's `urls.py`.
Warnings
- breaking The comments application was removed from Django core (django.contrib.comments) in Django 1.6. Projects upgrading from older Django versions must install and configure `django-contrib-comments` as a standalone package.
- gotcha The `django.contrib.sites` framework and the `SITE_ID` setting are mandatory dependencies for `django-contrib-comments` to function correctly.
- gotcha Database tables for comments will not be created without running Django migrations.
- gotcha Displaying comments in templates requires specific template tags and context.
Install
-
pip install django-contrib-comments
Imports
- Comment
from django.contrib.comments.models import Comment
from django_comments.models import Comment
- CommentForm
from django.contrib.comments.forms import CommentForm
from django_comments.forms import CommentForm
- get_model
from django.contrib.comments import get_model
from django_comments import get_model
Quickstart
import os
# --- In your project's settings.py ---
INSTALLED_APPS = [
# Required Django core apps
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.sites', # REQUIRED for django-contrib-comments
# The comments app itself
'django_comments',
# ... your other apps
]
SITE_ID = int(os.environ.get('DJANGO_SITE_ID', 1)) # REQUIRED for django-contrib-comments
# --- In your project's urls.py ---
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
# Include the comments app's URLs
path('comments/', include('django_comments.urls')),
# ... your application-specific URLs where comments will be rendered
]
# After configuring these, run: python manage.py migrate