6

django自带的评论模块

 2 years ago
source link: https://www.hi-roy.com/posts/django%E8%87%AA%E5%B8%A6%E7%9A%84%E8%AF%84%E8%AE%BA%E6%A8%A1%E5%9D%97/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

django自带的评论模块

2014-04-04

某个设计本来采用了一个比较不错的在线评论模块,不过答辩的时候丫居然不给网!!想偷个懒还是挺难啊….

那就用自带的评论模块吧,django版本是1.5.3。

首先把'django.contrib.comments'添加到INSTALLED_APPS中,然后添加url:

urlpatterns = patterns('',
......
url(r'^comments/', include('django.contrib.comments.urls')),
......
)

在需要显示评论的HTML中:

{% load comments %}
{% render_comment_list for post %} <!--显示所有属于这个文章的评论-->
{% render_comment_form for post %} <!--显示自带的添加评论的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="/blog/post/{{post.id}}"/> <!--这个是提交后的重定向,使用软编码总报错...-->
</form>

然后再进行CSS或者JS一类的特效美化就OK了。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK