django评论框架应用程序。它可以与任何给定的模型相关联。

django-comments-dab的Python项目详细描述


dab代表django ajax引导

django-comments-dab是django的注释应用程序 网站。

它允许您将注释功能与任何模型集成 有博客、图片等。

可以执行的操作列表:

  1. Post a new comment. (Authenticated)
  2. Reply to an existing comment. (Authenticated)
  3. Edit a comment you posted. (Authenticated)
  4. Delete a comment you posted. (Authenticated)
  • 所有操作都由ajax-jquery 3.2.1完成
  • bootstrap 4.1.1用于响应式设计的注释模板中。

安装

要求:

  1. django>=2.1.5
  2. django-widget-tweaks==1.4.2
  3. djangorestframework==3.8.2 # for API Framework
  4. Bootstrap 4.1.1
  5. 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:

  1. Add ^{tt3}$ to your installed_apps in your ^{tt4}$ file. It should be added after ^{tt5}$. and,
  2. Make sure that ^{tt6}$ is already included in installed_apps as well.
  3. ^{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个可选参数

  1. The instance of the model. (positional)
  2. Request object. (positional)
  3. oauth. (optional - Default is false)
  4. paginate. (optional - Default is false)
  5. 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 file

2. 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种方法可用于执行以下操作:

  1. Post a new comment. (Authenticated)
  2. Reply to an existing comment. (Authenticated)
  3. Edit a comment you posted. (Authenticated)
  4. Delete a comment you posted. (Authenticated)
  5. 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')

注释API操作:

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:

  • ^{tt32}$: is the model name of the content type that have comments associated with it.
  • ^{tt33}$: 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:

axios({method:"get",url:"http://127.0.0.1:8000/api/comments/?type=post&id=2",})

2- Post a comment and reply to an existing comment:

Authorization must be provided in the headers.

The data queries provided with the url are as above with one more parameter:

  • ^{tt34}$: is 0 or NOT PROVIDED for parent comments and for reply comments must be the id of parent comment

Example: posting a parent comment

axios({method:"post",url:"http://127.0.0.1:8000/api/comments/create/?type=post&id=2&parent_id=0",data:{content:"Hello comments"},headers:{Accept:"application/json","Content-Type":"application/json",Authorization:`Token ${token}`}})

3- Update a comment:

Authorization must be provided in the headers.

The url has no data queries in this action.

This action requires the ^{tt35}$ that you want to update:

axios({method:"put",url:"http://127.0.0.1:8000/api/comments/1",data:{content:"Update comment number 1 (id=1)"},headers:{Accept:"application/json","Content-Type":"application/json",Authorization:`Token ${token}`}})

4- Delete a comment:

Authorization must be provided in the headers.

The url has no data queries in this action.

This action requires the ^{tt35}$ that you want to delete:

axios({method:"delete",url:"http://127.0.0.1:8000/api/comments/1",headers:{Accept:"application/json","Content-Type":"application/json",Authorization:`Token ${token}`}})

风格定制:

1-引导类:

默认模板中使用的bs类现在可以从模板目录中进行自定义,如下所示:

  1. Create ^{tt3}$ folder inside your templates directory.
  2. 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文件:

如果要自定义“评论”应用程序的默认样式,可以执行以下步骤:

  1. Create a ^{tt40}$ file inside your ^{tt41}$ directory.
  2. The new created file will override the original file used in the app.

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java JavaFX 11可编辑组合框引发IndexOutOfBoundsException   java选择数组中的数组元素   java我从来没有找到创建2D ArrayList的正确方法   java JPA查找orderById的顶部数据,并按字符串过滤Id   使用java在ejabberd中进行xmpp外部身份验证   从ajax调用向java传递点运算符   java如何使用ReadWriteLock   使用Spring控制器和jQueryAjax的java重定向   java使JFrame中的JPanel可滚动   java如何用多个。jar库?   java EditText在RecyclerView中失去了对滚动的关注   java为什么我们必须扩展Servlet或GenericServlet或HttpServlet来创建Servlet应用程序?如果不扩展,我们可以开发Servlet应用程序吗?   使用递归java查找数组中的最大值   具有不同字段数的html表单的java域传输对象   java文本视图扩展;不支持操作异常   java如何使用iText的HTMLWorker类将多语言HTML字符串呈现为PDF