Django自带评论功能的基本使用
1. 模块安装
pip install django-contrib-comments
2. 注册APP
INSTALLED_APP=( #..., 'django_comments', 'django.contrib.sites', ) SITE_ID = 1 #需要设置此配置
3. 更新数据库
python manage.py migrate
4. 路由配置
urlpatterns = [ path(r'^comments/', include('django_comments.urls')), ]
5. 在模板中使用评论功能
# 加载 {% load comments %} # 评论列表 <h2>评论列表</h2> { % get_comment_list for [object] as comments % } { % for comment in comments % } <p>on {{comment.submit_date|date:”F,j,Y”}}, {{comment.user_name}} said: {{comment.comment|safe}}</p> { % endfor % } # 评论表单 <div> <h2>发表你的评论</h2> {% render_comment_form for [object] %} </div>