http://newliu.com/post/11/ css
http://mozillazg.com/2013/01/django-built-in-comments-framework.html html
http://www.cnblogs.com/cacique/archive/2012/10/03/2710803.html python
https://docs.djangoproject.com/en/1.5/ref/contrib/comments/#std:templatetag-render_comment_list 数据库
https://docs.djangoproject.com/en/1.5/ref/contrib/comments/moderation/ django
https://docs.djangoproject.com/en/1.5/ref/contrib/comments/example/ 安全
https://docs.djangoproject.com/en/1.5/ref/contrib/comments/custom/ app
comments库是django框架内置的一个评论库,官方文档地址:https://docs.djangoproject.com/en/dev/ref/contrib/comments/能够快捷的搭建出网站须要的评论系统。不过对这个库的评价彷佛褒贬不一,我本身在使用中的感觉是要想让这个库可以比较完美的工做,可能本身须要作较多的定制工做,有时想一想,还真不如本身重头写来的爽气。这里照例把本身的一些使用经验记录一下,以供参考。框架
1、激活步骤oop
添加APP:INSTALLED_APPS=(‘django.contrib.comments’,)post
更新数据库。执行命令:python manage.py syncdb
添加url。在urls.py中添加:(r’^comments/’, include(‘django.contrib.comments.urls’)),
2、如何在admin后台显示comments详情页面
将settings.py中的INSTALLED_APPS中的django.contrib.sites取消注释便可。
3、如何在模板中使用comments
在模板文件中加载comments这个模板标签:
{%load comments%}
4、如何在模板中显示评论
使用示例以下:
{%get_comment_list for [object] as [comment_list]%} {%for comment in comment_list%} <p>on {{comment.submit_date|date:”F,j,Y”}}, {{comment.user_name}} said: {{comment.comment|safe}}</p> {%endfor%}
5、给用户显示一个添加评论的表单
能够简单的使用内置的评论表单模板,示例以下:
<div id=’commentform’> <h2>发表你的评论</h2> {%render_comment_form for [object]%} </div>
只须要这个模板标签,就将comments系统与你的项目集成了。comments库将会使用内置的模板文件自动为你生成一个评论表单,该表单包含的字段将包括:
csrfmiddlewaretoken——django csrf中间件须要
content_type——
content_pk——ID值
timestamp——当前时间
security_hash——安全检测用
name——名称
email——邮箱
comment——内容
honeypot——防止机器乱填垃圾信息
6、自定义评论表单
很遗憾的说,comments库自带的表单实在太丑了,并且咱们总得对表单作一些自定义,使其符合咱们的需求。故咱们能够将整个评论表单自定义一下,只要在模板文件中使用get_comment_form这个模板标签便可获取一个可在模板中使用的表单对象。一个较为完整的示例以下:
{%get_comment_form for post as form%} <form action='{%comment_form_target%}' method='post'> {% csrf_token %} {{form.object_pk}} {{form.content_type}} {{form.timestamp}} {{form.security_hash}} <p><label for="id_name">姓名(必填):</label><input name="name" id="id_name"></p> <p><label for="id_email">邮箱(必填):</label><input name="email" id="id_email"></p> <p><label for="id_url">网站(可选):</label><input name="url" id="id_url"></p> <p><label for="id_comment">评论(必填):</label></p> <p><textarea id="id_comment" rows="10" cols="40" name="comment"></textarea></p> <p style="display:none;"><label for="id_honeypot">若是你在该字段中输入任何内容,那么你的评论就会被视为垃圾评论。</label> <input type="text" name="honeypot" id="id_honeypot"></p> <p><input name="post" value="发表" type="submit" /></p> <input type='hidden' name='next' value='{%url post_by_id post.id%}'/> </form>
可为其加上以下的CSS样式
<style type="text/css"> label{display:inline;float:left;width:100px;} input,textarea{width:340px;} textarea{height:80px;} input[type=submit]{width:120px;margin-left:300px;} </style>
关于示例中代码的一些解释:
1.用于生成评论提交地址。
<form action='{%comment_form_target%}' method='post'>
2.用于评论提交后的重定向。
<input type=”hidden” name=”next” value=”{%url my_comment_was_posted%}”/>
3.自定义表单时,必定要加上{% csrf_token %}这句。另外四个模板变量则是调用form.属性来生成那些隐藏的字段的值或名称,由于咱们看到当使用默认表单的时候,comments会自动帮咱们把9个字段所有收成好,故当咱们自定义表单时也须要补全全部字段,否则会提交失败。
{% csrf_token %} {{form.object_pk}} {{form.content_type}} {{form.timestamp}} {{form.security_hash}}
4.关于honeypot字段的说明。
这个字段是用于防止机器程序发布垃圾信息的。文档里的说法是:通常机器程序发布垃圾信息时,会把表单里的全部字段都填上,而这个字段一旦被填上则此信息将被判为spam,简单说这个字段是用来戏耍机器程序的,我不知道究竟有没有效实际效果。
7、若须要登陆才能显示发布评论的表单
示例以下:
{%if user.is_authenticated%} <h2>发表你的评论</h2> {%render_comment_form for object%} {%else%} 请<a href=”/accounts/login”>登陆</a>,或<a href=”/accounts/register”>注册</a>后再评论 {%endif%}
8、显示评论数量
{%get_comment_count for [object] as [comment_count]%}
9、评论的连接
{%for comment in comment_list%} <a href=”{%get_comment_permalink comment%}”> permalink for comment #{{forloop.counter}} </a> {%end for%}
10、评论生成后自动发邮件通知网站管理员
给评论系统再增长一个邮件通知系统,我是这样实现的:修改django的comments库的源代码,在python27/lib/site-packages/django/contrib/comments/views/comments.py中添加以下代码:
#coding:utf-8 from django.core.mail import send_mail from django.views.decorators.csrf import csrf_exempt if comment.is_public: send_mail( u’博客有新评论’, u’评论者:\n’+comment.user_name+u’\n\r评论内容:\n’+comment.comment+u’\n\r评论者邮箱:\n’+comment.user_email, ‘sender@example.com’, [‘admin@example.com’], )
先判断评论的is_public属性,由于若使用了akismet,则恶意评论的该属性为false,故网站收到恶意评论不会向管理员发送通知邮件。须要注意这个代码块要放在源码当中comment.save()这句的后面,否则得不到正确的is_public属性值。
11、自定义某些模板
若是评论表单未提交成功,则comments库会自动加载其源码中的comments/preview.html这个默认模板,提醒用户表单项有误。但须要注意的是这个模板会很丑陋,故你能够在本身的项目中复制这个模板(路径要保证是templates/comments/preview.html便可),重写你本身的提醒内容,加上本身设计的样式。