django评论框架应用程序。它可以与任何给定的模型相关联。
django-comments-dab的Python项目详细描述
dab代表django ajax引导
django-comments-dab是django的注释应用程序 网站。
它允许您将注释功能与任何模型集成 有博客、图片等。
可以执行的操作列表:
- Post a new comment. (Authenticated)
- Reply to an existing comment. (Authenticated)
- Edit a comment you posted. (Authenticated)
- Delete a comment you posted. (Authenticated)
- 所有操作都由ajax-jquery 3.2.1完成
- bootstrap 4.1.1用于响应式设计的注释模板中。
安装
要求:
- django>=2.1.5
- django-widget-tweaks==1.4.2
- djangorestframework==3.8.2 # for API Framework
- Bootstrap 4.1.1
- jQuery 3.2.1
安装:
可通过pip
进行安装$ pip install django-comments-dab
或者通过github上的源代码
$ git clone https://github.com/radi85/Comment.git $ cd Comment $ python setup.py install
设置和URL:
- Add ^{tt3}$ to your installed_apps in your ^{tt4}$ file. It should be added after ^{tt5}$. and,
- Make sure that ^{tt6}$ is already included in installed_apps as well.
- ^{tt7}$ shall be defined in the settings.
您的settings.py应该如下所示:
INSTALLED_APPS=('django.contrib.admin','django.contrib.auth',...'widget_tweaks','comment','rest_framework',# for API Framework..)LOGIN_URL='login'# or your actual url
在您的url.py:
urlpatterns=patterns(path('admin/',admin.site.urls),path('comment/',include('comment.urls')),...path('api/',include('comment.api.urls')),# for API Framework...)
迁移:
迁移评论应用程序:
$ python manage.py migrate comment
设置
步骤1
在models.py中添加字段comments(请注意字段名 必须是comments而不是comment)到其注释的模型 应添加(例如post)和适当的导入,如下所示:
fromdjango.contrib.contenttypes.fieldsimportGenericRelationfromcomment.modelsimportCommentclassPost(models.Model):author=models.ForeignKey(User)title=models.CharField(max_length=200)body=models.TextField()# the field name should be commentscomments=GenericRelation(Comment)
步骤2
get_comments标记使用2个位置参数和3个可选参数:
- The instance of the model. (positional)
- Request object. (positional)
- oauth. (optional - Default is false)
- paginate. (optional - Default is false)
- cpp (number of Comments Per Page - Default is 10)
1.基本用法:
include_static此标记将包括必需的jquery和javascript文件, 如果您已经使用了jquery,请确保它不是不支持ajax的slim版本。 include_bootstrap标记用于bootstrap-4.1.1(如果已经包含) 在你的项目中,去掉这个标签。
在模板(例如post detail.html)中添加以下模板标记,其中object是post model的实例。
{%loadcomment_tags%}# Loading the template tag{%get_commentsobjectrequest%}# Include all the comments belonging to a certain object
包含静态文件:
Comment应用程序有三个模板标记,用于应用程序所需的静态文件。 这些标记需要包含在基本模板的末尾。
- case 1:您的项目中已经有jquey,那么jquery文件下面应该包含以下标记:
<scriptsrc="https://code.jquery.com/jquery-3.3.1.js"></script> {% load comment_tags %} # Loading the template tag {% include_static %} # Include comment.js file only. {% include_bootstrap %} # Include bootstrap 4.1.1 - remove this line if BS 4.1.1 is already used in your project
- case 2:项目中没有jquery,则应包括以下标记:
{% load comment_tags %} # Loading the template tag {% include_static_jquery %} # Include mini jQuery 3.2.1 and required js file. {% include_bootstrap %} # Include bootstrap 4.1.1 - remove this line if BS 4.1.1 is already used in your project
2.高级用法:
1. Add pagination:
To add pagination to your comments, you need to pass two variables to the ^{tt12}$ tag. ^{tt16}$ must be set to ^{tt17}$ and set ^{tt18}$ var (number of comments per page - default is 10) to the desired number of comments per page. e.g. If you would like to have 5 comments per page, the ^{tt12}$ tag should look like this:
{%loadcomment_tags%}# Loading the template tag{%get_commentsobjectrequestpaginate=Truecpp=5%}# Include all the comments belonging to a certain object{%include_bootstrap%}# Include bootstrap 4.1.1 - remove this line if BS 4.1.1 is already used in your project{%include_static%}# Include jQuery 3.2.1 and required js file2. Integrate existing profile app with comments app:
If you have a profile model for the user and you would like to show the profile image on each comment, you need to do these two steps:
- Declare ^{tt20}$ and ^{tt21}$ variables in your ^{tt4}$ file.
(e.g if user profile app is called ^{tt23}$ and profile model is called ^{tt24}$) Update your ^{tt4}$ as follows:
PROFILE_APP_NAME='accounts'PROFILE_MODEL_NAME='UserProfile'# letter case insensitive
- Make sure that ^{tt26}$ method is defined in your profile model.
Update your ^{tt27}$ as follows:
fromdjango.urlsimportreverseclassUserProfile(models.Model):user=models.OneToOneField(User,on_delete=models.CASCADE)......# this method must be defined for appropriate url mapping in comments sectiondefget_absolute_url(self):returnreverse('profile_url_name')
Web API
django注释dab使用django rest框架公开一个web api,该api提供 通过web用户界面访问相同功能的开发人员。
有5种方法可用于执行以下操作:
- 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.
下面将解释这些操作。
设置:
要在内容类型(例如post模型)中集成注释api,请在serializers.py 对于post model add comments字段,如下所示:
fromrest_frameworkimportserializersfromcomment.modelsimportCommentfromcomment.api.serializersimportCommentSerializerclassPostSerializer(serializers.ModelSerializer):comments=serializers.SerializerMethodField()classMeta:model=Postfields=('id',......'comments')defget_comments(self,obj):comments_qs=Comment.objects.filter_by_object(obj)returnCommentSerializer(comments_qs,many=True).data
默认情况下,配置文件模型中的图像字段将包含在用户对象中 在json响应中。只有在前面提到的配置文件属性是 在settings.py中定义。如果您希望序列化配置文件模型中的更多字段 您需要在^{tt4}中显式声明COMMENT_PROFILE_API_FIELDS元组$ 如下所示:
PROFILE_APP_NAME='accounts'PROFILE_MODEL_NAME='userprofile'# the field names below must be similar to your profile model fieldsCOMMENT_PROFILE_API_FIELDS=('display_name','birth_date','image')
风格定制:
1-引导类:
默认模板中使用的bs类现在可以从模板目录中进行自定义,如下所示:
- Create ^{tt3}$ folder inside your templates directory.
- Create new template file ^{tt38}$ with the same name of the default template you wish to override BS classes in it.
例如,要覆盖comment和reply btn的bs类,请执行以下操作:
创建templates/comment/create_comment.html
{%extends"comment/create_comment.html"%}{%blockpost_btn_cls%}btnbtn-primarybtn-blockbtn-sm{%endblockpost_btn_cls%}
Read the Doc了解有关模板名和块标记名的详细信息。
2-css文件:
如果要自定义“评论”应用程序的默认样式,可以执行以下步骤:
- Create a ^{tt40}$ file inside your ^{tt41}$ directory.
- The new created file will override the original file used in the app.
注释API操作: