Django 博客评论系统替代多说 (一)
由 瞳人
发布于 Oct. 7, 2015, 4:51 p.m.
36 个评论
这篇文章里面将讲述如何利用 Django 以及一些大牛们的 Django 应用来配置一个最基本的本地博客评论.
依赖的 Django application
- Django 官方的评论应用 django-contrib-comments
- django-threadedcomments 扩展了官方的评论应用, 使得可以针对已有评论进行回复的应用.
开始动手
注意, 我博客的环境是 Django 1.8.x, 其他版本的配置安装可以查阅上述 Django application 的项目页面.
安装依赖项目.
1 | pip install django-contrib-comments django-threadedcomments |
修改 Project 中的 settings.py
.
1 2 3 4 5 6 7 8 9 10 | INSTALLED_APPS = ( ... 'threadedcomments', 'django_comments', 'django.contrib.sites', ) # 设置自定义的评论应用 COMMENTS_APP = 'threadedcomments' # 设置站点编号, 根据自己需求编号 SITE_ID = 1 |
修改 Project 中的 urls.py
.
1 2 3 4 | urlpatterns = [ ... url(r'^comments/', include('django_comments.urls')), ] |
然后在你需要加入评论系统的 template 中, 添加如下代码:
1 2 3 4 5 | {% load threadedcomments_tags %} .... <h2>Comments for {{ object.title }}:</h2> {% render_comment_list for object %} {% render_comment_form for object %} |
其中的 object
是你想要添加评论的主体对象, 例如在博客文章页面中,
这个 object
可能是 view.py
中传进来的 article
.
这时候, 访问博客文章页面, 会看见如下的评论框效果(我已经添加了两条评论).
但是, 显然很不美观. 而且虽然 threadedcomments 支持针对评论的回复, 但是基于默认配置无法使用该功能. 下一篇博客 Django 博客评论系统替代多说 (二) 中进行一些自定义.

围观者 March 17, 2016, 11:10 p.m. | Reply
问一下你啊,那个403怎么破?

额 April 22, 2016, 11:17 p.m. | Reply
e

brant-ruan July 26, 2016, 11:17 p.m. | Reply
您好,我按照您的方法(后来又按照官方的步骤)操作,总是出现一个问题:502 bad gateway
我使用nginx1.4+gunicorn19.6.0+django1.8.14
本地终端中提示的错误为:
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/gunicorn/workers/sync.py", line 135, in handle
self.handle_request(listener, req, client, addr)
File "/usr/local/lib/python2.7/dist-packages/gunicorn/workers/sync.py", line 191, in handle_request
six.reraise(*sys.exc_info())
File "/usr/local/lib/python2.7/dist-packages/gunicorn/workers/sync.py", line 176, in handle_request
respiter = self.wsgi(environ, resp.start_response)
......
File "/usr/lib/python2.7/logging/__init__.py", line 1271, in _log
self.handle(record)
File "/usr/lib/python2.7/logging/__init__.py", line 1281, in handle
self.callHandlers(record)
File "/usr/lib/python2.7/logging/__init__.py", line 1321, in callHandlers
hdlr.handle(record)
File "/usr/lib/python2.7/logging/__init__.py", line 749, in handle
self.emit(record)
File "/usr/local/lib/python2.7/dist-packages/django/utils/log.py", line 129, in emit
self.send_mail(subject, message, fail_silently=True, html_message=html_message)
File "/usr/local/lib/python2.7/dist-packages/django/utils/log.py", line 132, in send_mail
mail.mail_admins(subject, message, *args, connection=self.connection(), **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/core/mail/__init__.py", line 98, in mail_admins
mail.send(fail_silently=fail_silently)
File "/usr/local/lib/python2.7/dist-packages/django/core/mail/message.py", line 303, in send
return self.get_connection(fail_silently).send_messages([self])
File "/usr/local/lib/python2.7/dist-packages/django/core/mail/backends/smtp.py", line 100, in send_messages
new_conn_created = self.open()
File "/usr/local/lib/python2.7/dist-packages/django/core/mail/backends/smtp.py", line 58, in open
self.connection = connection_class(self.host, self.port, **connection_params)
File "/usr/lib/python2.7/smtplib.py", line 256, in __init__
(code, msg) = self.connect(host, port)
File "/usr/lib/python2.7/smtplib.py", line 316, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "/usr/lib/python2.7/smtplib.py", line 291, in _get_socket
return socket.create_connection((host, port), timeout)
File "/usr/lib/python2.7/socket.py", line 571, in create_connection
raise err
error: [Errno 111] Connection refused

brant-ruan July 27, 2016, 10:33 a.m. | Reply
今天又试了还是这样,我看错误中有mail。请问添加评论功能需要预先设置发送邮件功能吗?我没有设置邮件发送的

瞳人 July 27, 2016, 10:47 a.m. | Reply
如果就这个 django-threadedcomments 项目的话,是不需要设置 email 的,因为它不包含新评论发送邮件的功能。
看你这个代码像是 logging 出错了。因为是 logging 调用了发送 email。然后这个logging 好像又是 gunicorn 触发的。你直接用 python manage.py runserver 会报错吗?

brant-ruan July 27, 2016, 11:22 a.m. | Reply
您好,可能是gunicorn的问题,我使用runserver调试模式没有那个错误了,依然是您本文中的代码,其中
{% render_comment_form for post %}
会导致
Error during template rendering
In template /home/brant-ruan/web/my_blog/article/templates/post.html, error at line 22
__init__() got an unexpected keyword argument 'parent'
12 </p>
13 </header>
14
15 <div class="post-description">
16 {{ post.content|safe }}
17 </div>
18 </section>
19 </div>
20 <h2>Comments for {{ post.title }}</h2>
21 {% render_comment_list for post %}
22
{% render_comment_form for post %} (debug模式此句报错)
23
24 {% endblock %}
25
删除掉form那句后就没错了。不知是怎么回事?

瞳人 July 27, 2016, 11:31 a.m. | Reply
我确认一下,使用同样的代码,不做任何修改,不删除 form 那一句,使用 runserver 不会出错。是这样吗?

brant-ruan July 27, 2016, 11:38 a.m. | Reply
在runserver下不删除form会出错,就是上一条评论中给您回复的。删除后正常

brant-ruan July 27, 2016, 11:40 a.m. | Reply
您之前提到的gunicorn的log问题我自己再找找资料看,现在是render form会出错

瞳人 July 27, 2016, 11:45 a.m. | Reply
这个 gunicorn 的问题,应该是由于上面那个 template render 出错导致的,然后才需要log,但是可能 log 又没配置。
关于 template 的问题,报错是 parent 参数不对。这个 parent 是 threadedcomments 中用来记录你所回复的评论的。所以出错是不是由于你,没有设置 COMMENTS_APP = 'threadedcomments',或者没有 {% load threadedcomments_tags %}。

zero Sept. 25, 2016, 8:58 p.m. | Reply
django-threadedcomments 在最新的1.10.1上是不是有问题

瞳人 Sept. 28, 2016, 3:45 p.m. | Reply
我还没有试1.10。最近太忙了。。没时间更新环境。我现在用的还是1.9的。
你指的是什么问题?

dlwxxxdlw Oct. 8, 2016, 5:07 p.m. | Reply
In template /Users/dlw/Documents/DLW/Django/blogDemo/sblog/templates/sblog/blog_show.html, error at line 46
'RequestContext' object has no attribute 'META'
36 {% load threadedcomments_tags %}
37 {% get_comment_count for blog as comment_count %}
38 <a href="#cmt" title="comment"><i class="icon-comment offset1"></i>{{ comment_count }} </a>
39 </div>
40 </div>
41 </section>
42 </article>
43 </div>
44 {% block mycomments %}
45 <h2>Comments for {{ blog.title }}:</h2>
46 {% render_comment_list for blog %}
47 {% render_comment_form for blog %}
48 {% endblock %}
49 {% endblock %}
50
已向threadedcomments的作者反应,不知道怎么解决

123123 Sept. 28, 2017, 4:56 p.m. | Reply
ceshi reply

zero March 22, 2017, 4:54 p.m. | Reply
学习了

的 Aug. 21, 2017, 11:08 p.m. | Reply
弟弟


邻里派 March 24, 2018, 5:03 p.m. | Reply
终于成功了

zhou March 28, 2018, 3:54 p.m. | Reply
受益匪浅

zhou June 11, 2018, 9:43 p.m. | Reply
感谢博主

zhou June 11, 2018, 9:43 p.m. | Reply
!

孕妇面膜 July 18, 2018, 7:20 p.m. | Reply
感谢博主

jim Sept. 18, 2020, 11:29 a.m. | Reply
感谢
36 Comments