Skip to main content

A modern, ajax-based appearance for django_comments

Project description

django-fluent-comments

https://travis-ci.org/edoburu/django-fluent-comments.svg?branch=master https://img.shields.io/pypi/v/django-fluent-comments.svg https://img.shields.io/pypi/dm/django-fluent-comments.svg https://img.shields.io/badge/wheel-yes-green.svg https://img.shields.io/pypi/l/django-fluent-comments.svg https://coveralls.io/repos/edoburu/django-fluent-comments/badge.svg?branch=master

The django-fluent-comments module enhances the default appearance of the django_comments or django.contrib.comments application to be directly usable in web sites. The features are:

  • Ajax-based preview and posting of comments

  • Configurable form layouts using django-crispy-forms and settings to exclude fields.

  • Comment moderation, using Akismet integration and auto-closing after N days.

  • E-mail notification to the site managers of new comments.

  • Optional threaded comments support via django-threadedcomments.

The application is designed to be plug&play; installing it should already give a better comment layout.

Installation

First install the module and django_comments, preferably in a virtual environment. It can be installed from PyPI:

pip install django-fluent-comments

Or the current folder can be installed:

pip install .

Configuration

To use comments, the following settings are required:

INSTALLED_APPS += (
    'fluent_comments',
    'crispy_forms',
    'django_comments',
    'django.contrib.sites',
)

COMMENTS_APP = 'fluent_comments'

Add the following in urls.py:

urlpatterns += patterns('',
    url(r'^blog/comments/', include('fluent_comments.urls')),
)

Provide a template that displays the comments for the object and includes the required static files:

{% load comments %}

<link rel="stylesheet" type="text/css" href="{{ STATIC_URL }}fluent_comments/css/ajaxcomments.css" />
<script type="text/javascript" src="{{ STATIC_URL }}fluent_comments/js/ajaxcomments.js"></script>

{% render_comment_list for object %}
{% render_comment_form for object %}

The database can be created afterwards:

./manage.py syncdb
./manage.py runserver

Template for non-ajax pages

The templates which django_comments renders use a single base template for all layouts. This template is empty by default since it’s only serves as a placeholder. To complete the configuration of the comments module, create a comments/base.html file that maps the template blocks onto your website base template. For example:

{% extends "mysite/base.html" %}{% load i18n %}

{% block headtitle %}{% block title %}{% trans "Responses for page" %}{% endblock %}{% endblock %}

{% block main %}
    <div id="comments-wrapper">
        {% block content %}{% endblock %}
    </div>
{% endblock %}

In this example, the base template has a headtitle and main block, which contain the content and title blocks that django_comments needs to see. This application also outputs an extrahead block for a meta-refresh tag. The extrahead block can be included in the site base template directly, so it doesn’t have to be included in the comments/base.html file.

CSS form layout

Form layouts generally differ across web sites, hence this application doesn’t dictate a specific form layout. Instead, this application uses django-crispy-forms which allows configuration of the form appearance. By default, the forms can be rendered with 2 well known CSS frameworks:

  • Bootstrap The default template pack. The popular simple and flexible HTML, CSS, and Javascript for user interfaces from Twitter.

  • Uni-form Nice looking, well structured, highly customizable, accessible and usable forms.

The CRISPY_TEMPLATE_PACK setting can be used to switch between both layouts. For more information, see the django-crispy-forms documentation.

Both CSS frameworks have a wide range of themes available, which should give a good head-start to have a good form layout. In fact, we would encourage to adopt django-crispy-forms for all your applications to have a consistent layout across all your Django forms.

If your form CSS framework is not supported, you can create a template pack for it and submit a pull request to the django-crispy-forms authors for inclusion.

Hiding form fields

Form fields can be hidden using the following settings:

FLUENT_COMMENTS_EXCLUDE_FIELDS = ('name', 'email', 'url')
COMMENTS_APP = 'fluent_comments'

When django-threadedcomments in used, the title field can also be removed.

Comment moderation

Comment moderation can be enabled for the specific models using:

from fluent_comments.moderation import moderate_model
from myblog.models import BlogPost

moderate_model(BlogPost,
    publication_date_field='publication_date',
    enable_comments_field='enable_comments',
)

This code can be placed in a models.py file. The provided field names are optional. By providing the field names, the comments can be auto-moderated or auto-closed after a number of days since the publication date.

The following settings are available for comment moderation:

AKISMET_API_KEY = "your-api-key"
AKISMET_BLOG_URL = "http://example.com"        # Optional, to override auto detection
AKISMET_IS_TEST = False                        # Enable to make test runs

FLUENT_CONTENTS_USE_AKISMET = True             # Enabled by default when AKISMET_API_KEY is set.
FLUENT_COMMENTS_CLOSE_AFTER_DAYS = None        # Auto-close comments after N days
FLUENT_COMMENTS_MODERATE_AFTER_DAYS = None     # Auto-moderate comments after N days.
FLUENT_COMMENTS_AKISMET_ACTION = 'moderate'    # Set to 'moderate' or 'delete'

To use Akismet moderation, make sure the AKISMET_API_KEY setting is defined.

Python 3 notes

The akismet 0.2 release does not support Python 3. Hence, it’s only installed for Python 2 environments.

For Python 3 systems, install one of the forks from the Akismet library to have proper Python 3 support. For example by including the following in your requirements.txt:

-e git+https://github.com/allieus/python-akismet.git#egg=akismet

E-mail notification

By default, the MANAGERS of a Django site will receive an e-mail notification of new comments. This feature can be enabled or disabled using:

FLUENT_COMMENTS_USE_EMAIL_NOTIFICATION = True

The template comments/comment_notification_email.txt is used to generate the e-mail message.

Threaded comments

There is build-in support for django-threadedcomments in this module. It can be enabled using the following settings:

INSTALLED_APPS += (
    'threadedcomments',
)

COMMENTS_APP = 'fluent_comments'

The templates and admin interface adapt themselves automatically to show the threaded comments.

IP-Address detection

This package stores the remote IP of the visitor in the model, and passes it to Akismet. The IP Address is read from the REMOTE_ADDR meta field. In case your site is behind a HTTP proxy (e.g. using Gunicorn or a load balancer), this would make all comments appear to be posted from the load balancer IP.

The best and most secure way to fix this, is using WsgiUnproxy middleware in your wsgi.py:

from django.core.wsgi import get_wsgi_application
from django.conf import settings
from wsgiunproxy import unproxy

application = get_wsgi_application()
application = unproxy(trusted_proxies=settings.TRUSTED_X_FORWARDED_FOR_IPS)(application)

In your settings.py, you can define which hosts may pass the X-Forwarded-For header in the HTTP request. For example:

TRUSTED_X_FORWARDED_FOR_IPS = (
    '11.22.33.44',
    '192.168.0.1',
)

Contributing

This module is designed to be generic, and easy to plug into your site. In case there is anything you didn’t like about it, or think it’s not flexible enough, please let us know. We’d love to improve it!

If you have any other valuable contribution, suggestion or idea, please let us know as well because we will look into it. Pull requests are welcome too. :-)

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-fluent-comments-1.2.tar.gz (36.6 kB view details)

Uploaded Source

Built Distribution

django_fluent_comments-1.2-py2.py3-none-any.whl (43.9 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file django-fluent-comments-1.2.tar.gz.

File metadata

File hashes

Hashes for django-fluent-comments-1.2.tar.gz
Algorithm Hash digest
SHA256 9660dc9e496f232ffda437e3518e4a9ae434f00c4ba22c843dfe5fc74b428105
MD5 678148916a0e6d0f1f85c8676e08237c
BLAKE2b-256 f4c083db78e14b37e9caf8d2e93bb7b080a37eb689b1e686dd73fc0b3384c74d

See more details on using hashes here.

File details

Details for the file django_fluent_comments-1.2-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for django_fluent_comments-1.2-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 94ca831017ffda3079c2d2fe98c41c7b176b11ec03cc5f58ea6d59dfe71c4fd9
MD5 a880ef6e3b18e07c27bb0c00eab53704
BLAKE2b-256 4cee4bfbd09d5e1a429d4492e3733c3f7c16e20d7149a6c0966354d616ded1cf

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page