django 评论

2016/5/19 posted in  python

Django comments评论

安装

Django comments are deprecated in latest version. You can either install older django version (such as 1.5). Or the better solution is to install comments from external repository like so:

pip install django-contrib-comments

and then change all imports like this:

from django.contrib.comments.models import Comment # old
from django_comments.models import Comment # new

引入app的时候也会出现相关的问题,需要引入两个包

INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.sites',
'django_comments',
'blog',
'pagination',
'tagging',
]

需要单独的引入django.contrib.sitesdjango_comments 。 注意这里必须是django_comments 。 如果引入 django.contrib.comments 则会报错。

都引入完成后需要进行一次 manage.py migrate

使用comments

配置urls.py中的变量让comments 添加

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^blog/', include('blog.urls', namespace='blog')),
    url(r'^comments/', include('django_comments.urls')),
]

注意这里django_comments 的引入包不一样,mac下面需要使用这种引入方式。

这里的引入要引入到外层url里面,如果引入到里层url会报错,这一点要特别注意。

在setting.py 中 添加SITE_ID变量

SITE_ID = 1

测试一个添加comments的form

首先要在页面里面添加标签引入

{% load comments %}

然后添加一个评论form作为测试

{% render_comment_form for post %}

可以看到绑定到了post上了,后续看看如何表示

对comments进行样式添加和表示

如果采用上面的方式表示comments,只能在样式表里面根据他自带的样式来进行修改。或者改默认的site里面的模板才能修改样式,但是这种方式很多情况下面不会适用,所以我们可以才用自定义form的方式来进行form 的封装

封装方式:

<div class="separator"></div>
<h3>{% trans 'Send Us a Message' %}</h3>
<p>{% trans 'message content' %}</p>
                
{% get_comment_form for post as form %}
<div id="contactform">
  <form id="content" action="{% comment_form_target %}" method="post">
    {% csrf_token %}
    <label for="name" id="name_label">Name (required)</label>
    <input type="text" name="name" id="id_name" size="50" value="" class="text-input" />
    <label for="email" id="email_label">URL (required)</label>
    <input type="text" name="url" id="id_url" size="50" value="" class="text-input" />
    <label for="email" id="email_label">Email (required)</label>
    <input type="text" name="email" id="id_email" size="50" value="" class="text-input" />
    <label for="msg" id="msg_label">Message</label>
    <textarea rows="10" cols="40" name="comment" id="id_comment" class="text-input" maxlength="3000"></textarea>
    <br/>
    {{ form.content_type }}
    {{ form.object_pk }}
    {{ form.timestamp }}
    {{ form.security_hash }}
    <input type="submit" class="button" id="submit_btn" name="submit" value="Send Message"/>
    <input type="hidden" name="next" value="{% url 'blog:single' post.id %}" />
  </form>
  <div class="clear"></div>
</div>

可以将相应的标签命名为django-comments 需要的方式,根据自己的需要填充。
最上面的 {% get_comment_form for post as form %} 用于绑定评价到某个具体的对象上面。可以在评价对象里面看到评价值绑定到id上,所以没有办法实现动态的多对象绑定,我们需要将对象绑定在某一组对象上,或者我们将需要绑定的对象分开,才能实现在不同的对象上绑定。

下面的绑定绑到具体的id_name之类的上面就可以了,这样我们就可以添加相关的class来匹配相关的页面了。

在后面记得添加上默认的隐藏的对象,来把对象id、对象时间等添加到form里面。

最后一行<input type="hidden" name="next" value="{% url 'blog:single' post.id %}" /> 用于绑定回复之后的跳转。