Django Comment app. It can be associated with any given model.
Project description
django-comments-dab
dab stands for Django-Ajax-Bootstrap
django-comments-dab is a commenting application for Django-powered websites.
It allows you to integrate commenting functionality with any model you have e.g. blogs, pictures, video etc…
List of actions the authenticated user can do:
Post a new comment.
Reply to an existing comment.
Edit a comment you posted.
Delete a comment you posted.
All actions are done by ajax calls - JQuery 3.2.1
Bootstrap 4.1.1 is used in comment templates for responsive design.
Installation
Requirements:
django>=2.1
djangorestframework # only for API Framework
Bootstrap 4.1.1
jQuery 3.2.1
Installation:
Installation is available via pip
$ pip install django-comments-dab
or via source on github
$ git clone https://github.com/radi85/Comment.git $ cd Comment $ python setup.py install
Migrations:
Migrate comment app:
$ python manage.py migrate comment
Setup
Step 1 - Connecting comment model with the target model
In your models.py add the field comments as a GenericRelation field to the required model.
PS: Please note that field name must be comments NOT comment.
E.g. Post model, as shown below:
from django.contrib.contenttypes.fields import GenericRelation
from comment.models import Comment
class Post(models.Model):
author = models.ForeignKey(User)
title = models.CharField(max_length=200)
body = models.TextField()
# the field name should be comments
comments = GenericRelation(Comment)
Web API
django-comments-dab uses django-rest-framework to expose a Web API that provides developers with access to the same functionality offered through the web user interface.
There are 5 methods available to perform the following actions:
Post a new comment. (Authenticated)
Reply to an existing comment. (Authenticated)
Edit a comment you posted. (Authenticated)
Delete a comment you posted. (Authenticated)
Retrieve the list of comments and associated replies to a given content type and object ID.
These actions are explained below.
Setup:
To integrate the comment API in your content type (e.g Post model), in serializers.py for the Post model add comments field as shown below:
from rest_framework import serializers
from comment.models import Comment
from comment.api.serializers import CommentSerializer
class PostSerializer(serializers.ModelSerializer):
comments = serializers.SerializerMethodField()
class Meta:
model = Post
fields = ('id',
...
...
'comments')
def get_comments(self, obj):
comments_qs = Comment.objects.filter_parents_by_object(obj)
return CommentSerializer(comments_qs, many=True).data
By default all fields in profile model will be nested inside the user object in JSON response. This can only happen if the profile attributes are defined in your settings.py. In case you would like to serialize particular fields in the profile model you should explicitly declare the COMMENT_PROFILE_API_FIELDS tuple inside your settings.py:
PROFILE_APP_NAME = 'accounts'
PROFILE_MODEL_NAME = 'userprofile'
# the field names below must be similar to your profile model fields
COMMENT_PROFILE_API_FIELDS = ('display_name', 'birth_date', 'image')
Comment API actions:
1- Retrieve the list of comments and associated replies to a given content type and object ID:
This action can be performed by providing the url with data queries related to the content type.
Get request accepts 3 params:
type: is the model name of the content type that have comments associated with it.
id: is the id of an object of that model
For example if you are using axios to retrieve the comment list of second object (id=2) of a model (content type) called post. you can do the following:
$ curl 'http://localhost:8000/api/comments/?type=TYPE&id=ID'2- Post a comment or reply to an existing comment:
Authorization must be provided as a TOKEN or USERNAME:PASSWORD.
parent_id: is 0 or NOT PROVIDED for parent comments and for reply comments must be the id of parent comment
Example: posting a parent comment
$ curl -X POST -u USERNAME:PASSWORD -d "content=CONTENT" "http://localhost:8000/api/comments/create/?type=MODEL&id=ID&parent_id=0"3- Update a comment:
Authorization must be provided as a TOKEN or USERNAME:PASSWORD.
This action requires the comment.id that you want to update:
$ curl -X PUT -u USERNAME:PASSWORD -d "content=CONTENT" "http://localhost:8000/api/comments/ID/4- Delete a comment:
Authorization must be provided as a TOKEN or USERNAME:PASSWORD.
This action requires the comment.id that you want to delete:
$ curl -X DELETE -u USERNAME:PASSWORD "http://localhost:8000/api/comments/ID/
Style Customize:
1- Bootstrap classes:
BS class used in the default template can be now customized from within your templates directory as follows:
Create comment folder inside your templates directory.
Create new template file .html with the same name of the default template you wish to override BS classes in it.
for example to override the BS classes of comment and reply btn do the following:
create templates/comment/create_comment.html
{% extends "comment/create_comment.html" %}
{% block post_btn_cls %}
btn btn-primary btn-block btn-sm
{% endblock post_btn_cls %}
Read the Doc for more info about template names and block tags name.
2- CSS file:
If you want to customize the default style of comments app , you can do the following steps:
Create a comment.css file inside your static/css directory.
The new created file will override the original file used in the app.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Hashes for django-comments-dab-1.4.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 82af4b8550bf14c5c61a9bf1969e2ef96873f065a6915bd72006f9d607e989f5 |
|
MD5 | 44bdd12d61f43228d72a968bc11aca76 |
|
BLAKE2b-256 | 7bce098862793ffc0e7ca71a2cfe2acaaa4bd3df4500da057bf45cc1e7deb902 |
Comment Settings and urls:
your settings.py should look like the following:
In your urls.py: