首页
/ Django Threaded Comments 技术文档

Django Threaded Comments 技术文档

2024-12-23 04:28:12作者:魏侃纯Zoe

1. 安装指南

1.1 安装依赖

首先,通过 pip 安装 django-threadedcomments 包:

pip install django-threadedcomments

推荐在虚拟环境中安装此模块,以避免与其他项目冲突。

1.2 配置 settings.py

在项目的 settings.py 文件中添加以下配置:

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

COMMENTS_APP = 'threadedcomments'

threadedcomments 应用放在 django.contrib.comments 应用之前,以便替换默认的 comments/list.html 模板为线程视图。

1.3 配置 urls.py

确保在 urls.py 中配置了 django_comments 的 URL:

from django.conf.urls import url, include

urlpatterns += [
    url(r'^articles/comments/', include('django_comments.urls')),
]

1.4 提供模板

为显示评论的对象(如文章或博客条目)提供一个模板:

{% load threadedcomments_tags %}

<h2>Comments for {{ object.title }}:</h2>

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

2. 项目的使用说明

2.1 模板设计

为了更好地定制评论的显示效果,建议编写自己的 comments/list.html 模板,或者使用 comments/app/list.htmlcomments/app/model/list.html 的覆盖模板。

同时,确保覆盖 comments/base.html 模板,以便其他 django_comments 视图使用网站的设计风格。

2.2 示例应用

项目中提供了一个 example 应用,展示了基本的配置,包括一个基于 JavaScript 的回复表单,该表单会移动到访客回复的评论位置。

3. 项目API使用文档

3.1 模板标签

threadedcomments_tags 库是 comments 库的替代品,支持与 django_comments 相同的语法,并添加了一些额外的参数。

3.1.1 获取评论数量

{% get_comment_count for [object] as [varname] %}
{% get_comment_count for [object] as [varname] root_only %}

{% get_comment_count for [app].[model] [id] as [varname] %}
{% get_comment_count for [app].[model] [id] as [varname] root_only %}

3.1.2 获取评论列表

{% get_comment_list for [object] as [varname] %}
{% get_comment_list for [object] as [varname] flat %}
{% get_comment_list for [object] as [varname] root_only %}

3.1.3 渲染评论列表

{% render_comment_list for [object] %}
{% render_comment_list for [object] root_only %}

{% render_comment_list for [app].[model] [id] %}
{% render_comment_list for [app].[model] [id] root_only %}

3.1.4 获取评论表单

{% get_comment_form for [object] as [varname] %}
{% get_comment_form for [object] as [varname] with [parent_id] %}
{% get_comment_form for [app].[model] [id] as [varname] %}
{% get_comment_form for [app].[model] [id] as [varname] with [parent_id] %}

3.1.5 渲染评论表单

{% render_comment_form for [object] %}
{% render_comment_form for [object] with [parent_id] %}
{% render_comment_form for [app].[model] [id] %}
{% render_comment_form for [app].[model] [id] with [parent_id] %}

3.1.6 渲染整个评论树

{% for comment in comment_list|fill_tree|annotate_tree %}
    {% ifchanged comment.parent_id %}{% else %}</li>{% endifchanged %}
    {% if not comment.open and not comment.close %}</li>{% endif %}
    {% if comment.open %}<ul>{% endif %}

    <li id="c{{ comment.id }}">
        ...
    {% for close in comment.close %}</li></ul>{% endfor %}
{% endfor %}

fill_tree 过滤器用于分页,确保第一个评论的父评论也被包含。annotate_tree 过滤器为评论添加 openclose 属性。

4. 项目安装方式

4.1 通过 pip 安装

使用以下命令安装 django-threadedcomments

pip install django-threadedcomments

4.2 配置项目

按照上述步骤配置 settings.pyurls.py,并提供相应的模板文件。

通过以上步骤,您可以成功安装并使用 django-threadedcomments 项目,实现线程评论功能。

登录后查看全文
热门项目推荐

项目优选

收起
kernelkernel
deepin linux kernel
C
22
6
docsdocs
OpenHarmony documentation | OpenHarmony开发者文档
Dockerfile
217
2.23 K
flutter_flutterflutter_flutter
暂无简介
Dart
523
116
ohos_react_nativeohos_react_native
React Native鸿蒙化仓库
JavaScript
210
285
nop-entropynop-entropy
Nop Platform 2.0是基于可逆计算理论实现的采用面向语言编程范式的新一代低代码开发平台,包含基于全新原理从零开始研发的GraphQL引擎、ORM引擎、工作流引擎、报表引擎、规则引擎、批处理引引擎等完整设计。nop-entropy是它的后端部分,采用java语言实现,可选择集成Spring框架或者Quarkus框架。中小企业可以免费商用
Java
9
1
RuoYi-Vue3RuoYi-Vue3
🎉 (RuoYi)官方仓库 基于SpringBoot,Spring Security,JWT,Vue3 & Vite、Element Plus 的前后端分离权限管理系统
Vue
982
580
pytorchpytorch
Ascend Extension for PyTorch
Python
67
97
ops-mathops-math
本项目是CANN提供的数学类基础计算算子库,实现网络在NPU上加速计算。
C++
564
87
openHiTLSopenHiTLS
旨在打造算法先进、性能卓越、高效敏捷、安全可靠的密码套件,通过轻量级、可剪裁的软件技术架构满足各行业不同场景的多样化要求,让密码技术应用更简单,同时探索后量子等先进算法创新实践,构建密码前沿技术底座!
C
1.02 K
399
GLM-4.6GLM-4.6
GLM-4.6在GLM-4.5基础上全面升级:200K超长上下文窗口支持复杂任务,代码性能大幅提升,前端页面生成更优。推理能力增强且支持工具调用,智能体表现更出色,写作风格更贴合人类偏好。八项公开基准测试显示其全面超越GLM-4.5,比肩DeepSeek-V3.1-Terminus等国内外领先模型。【此简介由AI生成】
Jinja
33
0