Blog·Tanky WooABOUTRSS

简单记录一下django的评论框架comments和一些问题

10 May 2012
这篇博客是从旧博客 WordPress 迁移过来,内容可能存在转换异常。

官方介绍:

Django’s comments framework

Example of using the built-in comments app

To get started using the comments app, follow these steps:

首先需要导入自定义标签。

{\% load comments %\}

Once loaded you can use the template tags below.

在第一个例子getcommentcount中:

{\% get_comment_count for entry as comment_count %\}.

In the above, blog.entry is the app label and (lower-cased) model name of the model class.

另外各种tags就直接看Django’s comments framework就行了。

在使用过程中,在提交评论时总是提示这个问题:

CSRF verification failed. Request aborted

提示:

> > In general, this can occur when there is a genuine Cross Site Request Forgery, or when Django's CSRF mechanism has not been used correctly. For POST forms, you need to ensure: > > > > Your browser is accepting cookies. The view function uses RequestContext for the template, instead of Context. In the template, there is a {\% csrf_token %\} template tag inside each POST form that targets an internal URL. If you are not using CsrfViewMiddleware, then you must use csrf_protect on any views that use the csrf_token template tag, as well as those that accept the POST data. > >
在网上看到的基本都是这种解答([传送门](http://blog.csdn.net/feng88724/article/details/7221449)):
> > 1、在表单Form里加上{\% csrf_token %\} > > > > 2、在Settings里的MIDDLEWARE_CLASSES增加配置:(一般默认就有) > > > > 'django.middleware.csrf.CsrfViewMiddleware', > > > > #'django.middleware.csrf.CsrfResponseMiddleware', > >
估计1.4的版本有了变化,在网上查了半天,最后还是在官方document上找到了: [传送门](https://docs.djangoproject.com/en/1.4/ref/contrib/csrf/) 它是这么处理的:
``` from django.core.context_processors import csrf from django.shortcuts import render_to_response def my_view(request): c = {} c.update(csrf(request)) # ... view code here return render_to_response("a_template.html", c) ```


另外关于使用自带的评论栏和自定义评论栏可以参考这位朋友的:

Django学习笔记—Comments库的使用方法小记