django 評論

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 數據庫

Django’s comments framework

https://docs.djangoproject.com/en/1.5/ref/contrib/comments/moderation/ django

Generic comment moderation

https://docs.djangoproject.com/en/1.5/ref/contrib/comments/example/ 安全

Example of using the built-in comments app

https://docs.djangoproject.com/en/1.5/ref/contrib/comments/custom/ app

Customizing the comments framework

comments庫是django框架內置的一個評論庫,官方文檔地址:https://docs.djangoproject.com/en/dev/ref/contrib/comments/能夠快捷的搭建出網站須要的評論系統。不過對這個庫的評價彷佛褒貶不一,我本身在使用中的感覺是要想讓這個庫可以比較完美的工做,可能本身須要作較多的定製工做,有時想一想,還真不如本身重頭寫來的爽氣。這裏照例把本身的一些使用經驗記錄一下,以供參考。框架

1、激活步驟oop

  1. 添加APP:INSTALLED_APPS=(‘django.contrib.comments’,)post

  2. 更新數據庫。執行命令:python manage.py syncdb 

  3. 添加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庫將會使用內置的模板文件自動爲你生成一個評論表單,該表單包含的字段將包括:

  1. csrfmiddlewaretoken——django csrf中間件須要

  2. content_type——

  3. content_pk——ID值

  4. timestamp——當前時間

  5. security_hash——安全檢測用

  6. name——名稱

  7. email——郵箱

  8. comment——內容

  9. 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便可),重寫你本身的提醒內容,加上本身設計的樣式。

相關文章
相關標籤/搜索